本文整理汇总了Golang中github.com/app-kit/go-appkit.Response.GetError方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.GetError方法的具体用法?Golang Response.GetError怎么用?Golang Response.GetError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.Response
的用法示例。
在下文中一共展示了Response.GetError方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ServerErrorMiddleware
func ServerErrorMiddleware(registry kit.Registry, r kit.Request, response kit.Response) (kit.Response, bool) {
err := response.GetError()
if err == nil {
return nil, false
}
status := 500
// If the error is an apperror, and it contains a status,
// set it as the http status of the response.
if apperr, ok := err.(apperror.Error); ok {
if apperr.GetStatus() != 0 {
status = apperr.GetStatus()
}
}
response.SetHttpStatus(status)
if response.GetRawData() != nil || response.GetRawDataReader() != nil {
return nil, false
}
httpRequest := r.GetHttpRequest()
apiPrefix := "/" + registry.Config().UString("api.prefix", "api")
isApiRequest := strings.HasPrefix(httpRequest.URL.Path, apiPrefix)
if isApiRequest {
return nil, false
}
data := map[string]interface{}{"errors": []error{response.GetError()}}
tpl := defaultErrorTpl()
tplPath := registry.Config().UString("frontend.errorTemplate")
if tplPath != "" {
t, err := template.ParseFiles(tplPath)
if err != nil {
registry.Logger().Fatalf("Could not parse error template at '%v': %v", tplPath, err)
} else {
tpl = t
}
}
var buffer *bytes.Buffer
if err := tpl.Execute(buffer, data); err != nil {
registry.Logger().Fatalf("Could not render error template: %v\n", err)
response.SetRawData([]byte("Server error"))
} else {
response.SetRawData(buffer.Bytes())
}
return nil, false
}
示例2: SerializeResponse
// SerializeResponse converts a response with model data into the target format.
func (s *Serializer) SerializeResponse(response kit.Response) (interface{}, apperror.Error) {
transData := response.GetTransferData()
if transData == nil {
transData = &kit.AppTransferData{}
data := response.GetData()
if model, ok := data.(kit.Model); ok {
transData.SetModels([]kit.Model{model})
} else if models, ok := data.([]kit.Model); ok {
transData.SetModels(models)
} else {
transData.SetData(data)
}
}
// Append response metadata.
if meta := response.GetMeta(); meta != nil {
transMeta := transData.GetMeta()
if transMeta != nil {
for key, val := range meta {
transMeta[key] = val
}
} else {
transData.SetMeta(meta)
}
}
// If a response error is defined, prepend it to all errors that might have been in
// TransferData.
if err := response.GetError(); err != nil {
oldErrs := transData.GetErrors()
transData.SetErrors(append([]apperror.Error{err}, oldErrs...))
}
data, err := s.SerializeTransferData(transData)
if err != nil {
return nil, err
}
return data, nil
}
示例3: convertResponse
func convertResponse(response kit.Response) *turnpike.CallResult {
result := &turnpike.CallResult{}
if response.GetError() != nil {
result.Err = turnpike.URI(response.GetError().GetCode())
}
result.Kwargs = map[string]interface{}{
"data": response.GetData(),
"meta": response.GetMeta(),
"errors": []error{response.GetError()},
}
return result
}
示例4: RequestLoggerMiddleware
func RequestLoggerMiddleware(registry kit.Registry, r kit.Request, response kit.Response) (kit.Response, bool) {
// Calculate time taken.
rawStarted, ok1 := r.GetContext().Get("startTime")
rawFinished, ok2 := r.GetContext().Get("endTime")
timeTaken := int64(-1)
if ok1 && ok2 {
started := rawStarted.(time.Time)
finished := rawFinished.(time.Time)
timeTaken = int64(finished.Sub(started) / time.Millisecond)
}
// Log the request.
method := r.GetHttpMethod()
path := r.GetPath()
if response.GetError() != nil {
registry.Logger().WithFields(logrus.Fields{
"frontend": r.GetFrontend(),
"action": "request",
"method": method,
"path": path,
"status": response.GetHttpStatus(),
"err": response.GetError(),
"milliseconds": timeTaken,
}).Errorf("%v: %v - %v - %v", response.GetHttpStatus(), method, path, response.GetError())
} else {
registry.Logger().WithFields(logrus.Fields{
"frontend": r.GetFrontend(),
"action": "request",
"method": method,
"path": path,
"status": response.GetHttpStatus(),
"milliseconds": timeTaken,
}).Debugf("%v: %v - %v", response.GetHttpStatus(), method, path)
}
return nil, false
}
示例5: RespondWithJson
func RespondWithJson(w http.ResponseWriter, response kit.Response) {
code := 200
respData := map[string]interface{}{
"data": response.GetData(),
}
if response.GetError() != nil {
errs := []error{response.GetError()}
additionalErrs := response.GetError().GetErrors()
if additionalErrs != nil {
for _, err := range additionalErrs {
if apiErr, ok := err.(apperror.Error); ok && apiErr.IsPublic() {
errs = append(errs, apiErr)
}
}
}
respData["errors"] = errs
code = 500
}
output, err2 := json.Marshal(respData)
if err2 != nil {
code = 500
respData = map[string]interface{}{
"errors": []error{
apperror.Wrap(err2, "json_encode_error"),
},
}
output, _ = json.Marshal(respData)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(output)
}