// Attaches an error to the current context. The error is pushed to a list of errors.
// It's a good idea to call Error for each error that occurred during the resolution of a request.
// A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
func(c*Context)Error(errerror,metainterface{}){
c.ErrorTyped(err,ErrorTypeExternal,meta)
}
func(c*Context)LastError()error{
s:=len(c.Errors)
ifs>0{
returnerrors.New(c.Errors[s-1].Err)
}else{
returnnil
}
}
/************************************/
/******** METADATA MANAGEMENT********/
/************************************/
// Sets a new pair key/value just for the specified context.
// It also lazy initializes the hashmap.
func(c*Context)Set(keystring,iteminterface{}){
ifc.Keys==nil{
c.Keys=make(map[string]interface{})
}
c.Keys[key]=item
}
// Get returns the value for the given key or an error if the key does not exist.
func(c*Context)Get(keystring)(interface{},error){
ifc.Keys!=nil{
item,ok:=c.Keys[key]
ifok{
returnitem,nil
}
}
returnnil,errors.New("Key does not exist.")
}
// MustGet returns the value for the given key or panics if the value doesn't exist.
func(c*Context)MustGet(keystring)interface{}{
value,err:=c.Get(key)
iferr!=nil||value==nil{
log.Panicf("Key %s doesn't exist",key)
}
returnvalue
}
/************************************/
/******** ENCOGING MANAGEMENT********/
/************************************/
// This function checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding
// "application/xml" --> XML binding
// else --> returns an error
// if Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer.Like ParseBody() but this method also writes a 400 error if the json is not valid.