本文整理汇总了Golang中github.com/ardanlabs/kit/log.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Update
// Update updates the form gallery in the MongoDB database
// collection.
func Update(context interface{}, db *db.DB, id string, gallery *Gallery) error {
log.Dev(context, "Update", "Started : Gallery[%s]", id)
if !bson.IsObjectIdHex(id) {
log.Error(context, "Update", ErrInvalidID, "Completed")
return ErrInvalidID
}
if err := gallery.Validate(); err != nil {
log.Error(context, "Update", err, "Completed")
return err
}
objectID := bson.ObjectIdHex(id)
gallery.DateUpdated = time.Now()
f := func(c *mgo.Collection) error {
log.Dev(context, "Update", "MGO : db.%s.update(%s, %s)", c.Name, mongo.Query(objectID), mongo.Query(gallery))
return c.UpdateId(objectID, gallery)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "Update", err, "Completed")
return err
}
log.Dev(context, "Update", "Completed")
return nil
}
示例2: List
// List retrives the form galleries for a given form from the MongoDB database
// collection.
func List(context interface{}, db *db.DB, formID string) ([]Gallery, error) {
log.Dev(context, "List", "Started")
if !bson.IsObjectIdHex(formID) {
log.Error(context, "List", ErrInvalidID, "Completed")
return nil, ErrInvalidID
}
formObjectID := bson.ObjectIdHex(formID)
var galleries = make([]Gallery, 0)
f := func(c *mgo.Collection) error {
q := bson.M{
"form_id": formObjectID,
}
log.Dev(context, "List", "MGO : db.%s.find(%s)", c.Name, mongo.Query(q))
return c.Find(q).All(&galleries)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "List", err, "Completed")
return nil, err
}
if err := hydrateMany(context, db, galleries); err != nil {
log.Error(context, "List", err, "Completed")
return nil, err
}
log.Dev(context, "List", "Completed")
return galleries, nil
}
示例3: GetAll
// GetAll retrieves a list of regexs.
func GetAll(context interface{}, db *db.DB, tags []string) ([]Regex, error) {
log.Dev(context, "GetAll", "Started : Tags[%v]", tags)
key := "grs" + strings.Join(tags, "-")
if v, found := cache.Get(key); found {
rgxs := v.([]Regex)
log.Dev(context, "GetAll", "Completed : CACHE : Rgxs[%d]", len(rgxs))
return rgxs, nil
}
var rgxs []Regex
f := func(c *mgo.Collection) error {
log.Dev(context, "GetAll", "MGO : db.%s.find({}).sort([\"name\"])", c.Name)
return c.Find(nil).All(&rgxs)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
if err == mgo.ErrNotFound {
err = ErrNotFound
}
log.Error(context, "GetAll", err, "Completed")
return nil, err
}
if rgxs == nil {
log.Error(context, "GetAll", ErrNotFound, "Completed")
return nil, ErrNotFound
}
cache.Set(key, rgxs, gc.DefaultExpiration)
log.Dev(context, "GetAll", "Completed : Rgxs[%d]", len(rgxs))
return rgxs, nil
}
示例4: Remove
// Remove removes an item into the items collection and remove any
// corresponding quads from the graph database.
func Remove(context interface{}, db *db.DB, graph *cayley.Handle, itemID string) error {
log.Dev(context, "Remove", "Started : ID[%s]", itemID)
// Get the item from the items collection.
items, err := item.GetByIDs(context, db, []string{itemID})
if err != nil {
log.Error(context, "Remove", err, "Completed")
return err
}
// Prepare the item map data.
itmMap := map[string]interface{}{
"item_id": items[0].ID,
"type": items[0].Type,
"version": items[0].Version,
"data": items[0].Data,
}
// Remove the corresponding relationships from the graph.
if err := wire.RemoveFromGraph(context, db, graph, itmMap); err != nil {
log.Error(context, "Remove", err, "Completed")
return err
}
// Delete the item.
if err := item.Delete(context, db, itemID); err != nil {
log.Error(context, "Remove", err, "Completed")
return err
}
log.Dev(context, "Remove", "Completed")
return nil
}
示例5: Auth
// Auth handles token authentication.
func Auth(h app.Handler) app.Handler {
// Check if authentication is turned off.
on, err := cfg.Bool(cfgAuth)
if err == nil && !on {
return func(c *app.Context) error {
log.Dev(c.SessionID, "Auth", "******> Authentication Off")
return h(c)
}
}
// Turn authentication on.
return func(c *app.Context) error {
token := c.Request.Header.Get("Authorization")
log.Dev(c.SessionID, "Auth", "Started : Token[%s]", token)
if len(token) < 5 || token[0:5] != "Basic" {
log.Error(c.SessionID, "Auth", app.ErrNotAuthorized, "Validating token")
return app.ErrNotAuthorized
}
var err error
if c.User, err = auth.ValidateWebToken(c.SessionID, c.DB, token[6:]); err != nil {
log.Error(c.SessionID, "Auth", err, "Validating token")
return app.ErrNotAuthorized
}
log.Dev(c.SessionID, "Auth", "Completed")
return h(c)
}
}
示例6: Create
// Create adds a new Submission based on a given Form into
// the MongoDB database collection.
func Create(context interface{}, db *db.DB, formID string, submission *Submission) error {
log.Dev(context, "Create", "Started : Form[%s]", formID)
if !bson.IsObjectIdHex(formID) {
log.Error(context, "Create", ErrInvalidID, "Completed")
return ErrInvalidID
}
if err := submission.Validate(); err != nil {
return err
}
// FIXME: handle Number field maybe with https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/ to resolve race condition
count, err := Count(context, db, formID)
if err != nil {
log.Error(context, "Create", err, "Completed")
return err
}
submission.Number = count + 1
f := func(c *mgo.Collection) error {
log.Dev(context, "Create", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(submission))
return c.Insert(submission)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "Create", err, "Completed")
return err
}
log.Dev(context, "Create", "Completed")
return nil
}
示例7: CreateWebToken
// CreateWebToken return a token and session that can be used to authenticate a user.
func CreateWebToken(context interface{}, db *db.DB, u *User, expires time.Duration) (string, error) {
log.Dev(context, "CreateWebToken", "Started : PublicID[%s]", u.PublicID)
// Do we have a valid session right now?
s, err := session.GetByLatest(context, db, u.PublicID)
if err != nil && err != mgo.ErrNotFound {
log.Error(context, "CreateUser", err, "Completed")
return "", err
}
// If we don't have one or it has been expired create
// a new one.
if err == mgo.ErrNotFound || s.IsExpired(context) {
if s, err = session.Create(context, db, u.PublicID, expires); err != nil {
log.Error(context, "CreateUser", err, "Completed")
return "", err
}
}
// Set the return arguments though we will explicitly
// return them. Don't want any confusion.
token, err := u.WebToken(s.SessionID)
if err != nil {
log.Error(context, "CreateUser", err, "Completed")
return "", err
}
log.Dev(context, "CreateWebToken", "Completed : WebToken[%s]", token)
return token, nil
}
示例8: GetUserWebToken
// GetUserWebToken return a token if one exists and is valid.
func GetUserWebToken(context interface{}, db *db.DB, publicID string) (string, error) {
log.Dev(context, "GetUserWebToken", "Started : PublicID[%s]", publicID)
// Do we have a valid session right now?
s, err := session.GetByLatest(context, db, publicID)
if err != nil {
log.Error(context, "GetUserWebToken", err, "Completed")
return "", err
}
// If it is expired return failure.
if s.IsExpired(context) {
err := errors.New("Session expired.")
log.Error(context, "GetUserWebToken", err, "Completed")
return "", err
}
// Pull the user information.
u, err := GetUserByPublicID(context, db, publicID, true)
if err != nil {
log.Error(context, "GetUserWebToken", err, "Completed")
return "", err
}
// Generate a token that works right now.
token, err := u.WebToken(s.SessionID)
if err != nil {
log.Error(context, "GetUserWebToken", err, "Completed")
return "", err
}
log.Dev(context, "GetUserWebToken", "Completed : WebToken[%s]", token)
return token, nil
}
示例9: UpdateUserPassword
// UpdateUserPassword updates an existing user's password and token in the database.
func UpdateUserPassword(context interface{}, db *db.DB, u *User, password string) error {
log.Dev(context, "UpdateUserPassword", "Started : PublicID[%s]", u.PublicID)
if err := u.Validate(); err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
if len(password) < 8 {
err := errors.New("Invalid password length")
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
newPassHash, err := crypto.BcryptPassword(u.PrivateID + password)
if err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": u.PublicID}
upd := bson.M{"$set": bson.M{"password": newPassHash, "modified_at": time.Now().UTC()}}
log.Dev(context, "UpdateUserPassword", "MGO : db.%s.Update(%s, CAN'T SHOW)", c.Name, mongo.Query(q))
return c.Update(q, upd)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
log.Dev(context, "UpdateUserPassword", "Completed")
return nil
}
示例10: TransformRow
// TransformRow transform a row of data into the coral schema
func TransformRow(row map[string]interface{}, strategyTableName string) (interface{}, []map[string]interface{}, error) { // id row, transformation, error
var newRows []map[string]interface{}
var err error
table := strategy.GetEntities()[strategyTableName]
idField := GetID(strategyTableName)
id := row[idField]
if table.Local == "" {
return "", nil, fmt.Errorf("No table %v found in the strategy file.", table)
}
// if has an array field type array
if strategy.HasArrayField(table) {
newRows, err = transformRowWithArrayField(strategyTableName, row, table.Fields)
if err != nil {
log.Error(uuid, "fiddler.transformRow", err, "Transform the row into several coral documents.")
}
} else {
newRow, err := transformRow(strategyTableName, row, table.Fields)
if err != nil {
log.Error(uuid, "fiddler.transformRow", err, "Transform the row into coral.")
return id, nil, err
}
newRows = append(newRows, newRow)
}
return id, newRows, err
}
示例11: saveResult
// saveResult processes the $save command for this result.
func saveResult(context interface{}, save map[string]interface{}, results []bson.M, data map[string]interface{}) error {
// {"$map": "list"}
// Capture the key and value and process the save.
for cmd, value := range save {
name, ok := value.(string)
if !ok {
err := fmt.Errorf("Save key \"%v\" is a %T but must be a string", value, value)
log.Error(context, "saveResult", err, "Extracting save key")
return err
}
switch cmd {
// Save the results into the map under the specified key.
case "$map":
log.Dev(context, "saveResult", "Saving result to map[%s]", name)
data[name] = results
return nil
default:
err := fmt.Errorf("Invalid save location %q", cmd)
log.Error(context, "saveResult", err, "Nothing saved")
return err
}
}
err := errors.New("Missing save document")
log.Error(context, "saveResult", err, "Nothing saved")
return err
}
示例12: main
func main() {
const context = "main"
// Create a task value for execution.
t := Task{
Name: "test task",
}
// Start the job running with a specified duration.
if err := runner.Run(context, time.Second, &t); err != nil {
switch err {
case runner.ErrTimeout:
// The task did not finish within the specified duration.
log.Error(context, "main", err, "Task timeout")
case runner.ErrSignaled:
// The user hit <control> c and we shutdown early.
log.Error(context, "main", err, "Shutdown early")
default:
// An error occurred in the processing of the task.
log.Error(context, "main", err, "Processing error")
}
os.Exit(1)
}
log.User(context, "main", "Completed")
}
示例13: UpdateStatus
// UpdateStatus updates a form submissions status inside the MongoDB database
// collection.
func UpdateStatus(context interface{}, db *db.DB, id, status string) (*Submission, error) {
log.Dev(context, "UpdateStatus", "Started : Submission[%s]", id)
if !bson.IsObjectIdHex(id) {
log.Error(context, "UpdateStatus", ErrInvalidID, "Completed")
return nil, ErrInvalidID
}
objectID := bson.ObjectIdHex(id)
f := func(c *mgo.Collection) error {
u := bson.M{
"$set": bson.M{
"status": status,
"date_updated": time.Now(),
},
}
log.Dev(context, "UpdateStatus", "MGO : db.%s.update(%s, %s)", c.Name, mongo.Query(objectID), mongo.Query(u))
return c.UpdateId(objectID, u)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "UpdateStatus", err, "Completed")
return nil, err
}
submission, err := Retrieve(context, db, id)
if err != nil {
log.Error(context, "UpdateStatus", err, "Completed")
return nil, err
}
log.Dev(context, "UpdateStatus", "Completed")
return submission, nil
}
示例14: Count
// Count returns the count of current submissions for a given
// form id in the Form Submissions MongoDB database collection.
func Count(context interface{}, db *db.DB, formID string) (int, error) {
log.Dev(context, "Count", "Completed : Form[%s]", formID)
if !bson.IsObjectIdHex(formID) {
log.Error(context, "Count", ErrInvalidID, "Completed")
return 0, ErrInvalidID
}
formObjectID := bson.ObjectIdHex(formID)
var count int
f := func(c *mgo.Collection) error {
var err error
q := bson.M{
"form_id": formObjectID,
}
log.Dev(context, "Count", "MGO : db.%s.find(%s).count()", c.Name, mongo.Query(q))
count, err = c.Find(q).Count()
return err
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "Count", err, "Completed")
return 0, err
}
log.Dev(context, "Count", "Completed")
return count, nil
}
示例15: RetrieveMany
// RetrieveMany retrieves a list of Submission's from the MongoDB database collection.
func RetrieveMany(context interface{}, db *db.DB, ids []string) ([]Submission, error) {
log.Dev(context, "RetrieveMany", "Started")
var objectIDs = make([]bson.ObjectId, len(ids))
for i, id := range ids {
if !bson.IsObjectIdHex(id) {
log.Error(context, "RetrieveMany", ErrInvalidID, "Completed")
return nil, ErrInvalidID
}
objectIDs[i] = bson.ObjectIdHex(id)
}
var submissions []Submission
f := func(c *mgo.Collection) error {
q := bson.M{
"_id": bson.M{
"$in": objectIDs,
},
}
log.Dev(context, "RetrieveMany", "MGO : db.%s.find(%s)", c.Name, mongo.Query(q))
return c.Find(q).All(&submissions)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "RetrieveMany", err, "Completed")
return nil, err
}
log.Dev(context, "RetrieveMany", "Started")
return submissions, nil
}