1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package middleware
- import (
- "github.com/gogf/gf/v2/errors/gcode"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/net/ghttp"
- "net/http"
- )
- // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
- //
- // This Source Code Form is subject to the terms of the MIT License.
- // If a copy of the MIT was not distributed with this file,
- // You can obtain one at https://github.com/gogf/gf.
- // DefaultHandlerResponse is the default implementation of HandlerResponse.
- type DefaultHandlerResponse struct {
- Code int `json:"error_code" dc:"Error code"`
- Message string `json:"error_msg" dc:"Error message"`
- Data interface{} `json:"data" dc:"Result data for certain request according API definition"`
- }
- // MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
- func MiddlewareHandlerResponse(r *ghttp.Request) {
- r.Middleware.Next()
- // There's custom buffer content, it then exits current handler.
- if r.Response.BufferLength() > 0 {
- return
- }
- var (
- msg string
- err = r.GetError()
- res = r.GetHandlerResponse()
- code = gerror.Code(err)
- )
- if err != nil {
- if code == gcode.CodeNil {
- code = gcode.CodeInternalError
- }
- msg = err.Error()
- res = nil //异常置空返回值
- r.Response.ClearBuffer()
- } else {
- if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
- msg = http.StatusText(r.Response.Status)
- switch r.Response.Status {
- case http.StatusNotFound:
- code = gcode.CodeNotFound
- case http.StatusForbidden:
- code = gcode.CodeNotAuthorized
- default:
- code = gcode.CodeUnknown
- }
- // It creates error as it can be retrieved by other middlewares.
- err = gerror.NewCode(code, msg)
- r.SetError(err)
- } else {
- code = gcode.CodeOK
- r.Response.WriteJson(res)
- return
- }
- }
- r.Response.WriteJson(DefaultHandlerResponse{
- Code: code.Code(),
- Message: msg,
- Data: res,
- })
- }
|