You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.7 KiB
85 lines
2.7 KiB
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"
|
|
"vistor/internal/libutil"
|
|
)
|
|
|
|
// DefaultHandlerResponse is the default implementation of HandlerResponse.
|
|
type DefaultHandlerResponse struct {
|
|
Code int `json:"code" dc:"Error code"`
|
|
Message string `json:"message" dc:"Error message"`
|
|
Data interface{} `json:"data" dc:"Result data for certain request according API definition"`
|
|
}
|
|
|
|
// HandlerResponse is the default middleware handling handler response object and its error.
|
|
func HandlerResponse(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 err.Error() != "" {
|
|
msg = err.Error()
|
|
} else {
|
|
msg = http.StatusText(code.Code())
|
|
}
|
|
} else if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
|
|
msg = http.StatusText(r.Response.Status)
|
|
switch r.Response.Status {
|
|
case http.StatusBadRequest:
|
|
code = StatusBadRequest
|
|
case http.StatusUnauthorized:
|
|
code = StatusUnauthorized
|
|
case http.StatusNotFound:
|
|
code = StatusNotFound
|
|
case http.StatusMethodNotAllowed:
|
|
code = StatusMethodNotAllowed
|
|
case http.StatusForbidden:
|
|
code = StatusForbidden
|
|
case http.StatusUnprocessableEntity:
|
|
code = StatusUnprocessableEntity
|
|
case http.StatusTooManyRequests:
|
|
code = StatusTooManyRequests
|
|
case http.StatusPaymentRequired:
|
|
code = StatusPaymentRequired
|
|
default:
|
|
code = StatusInternalServerError
|
|
}
|
|
} else {
|
|
code = StatusOK
|
|
msg = http.StatusText(code.Code())
|
|
}
|
|
|
|
r.Response.WriteJson(DefaultHandlerResponse{
|
|
Code: libutil.ReturnCode(code.Code()),
|
|
Message: msg,
|
|
Data: res,
|
|
})
|
|
}
|
|
|
|
var (
|
|
StatusOK = gcode.New(200, "OK", "")
|
|
StatusBadRequest = gcode.New(400, "Bad Request", "Bad Request")
|
|
StatusPaymentRequired = gcode.New(402, "Payment Required", "Payment Required")
|
|
StatusUnauthorized = gcode.New(401, "Unauthorized", "Unauthorized")
|
|
StatusForbidden = gcode.New(403, "Forbidden", "Forbidden")
|
|
StatusNotFound = gcode.New(404, "Not Found", "Resource does not exist")
|
|
StatusMethodNotAllowed = gcode.New(405, "Method Not Allowed", "Method Not Allowed")
|
|
StatusUnprocessableEntity = gcode.New(422, "Unprocessable Entity", "Data verification failure")
|
|
StatusTooManyRequests = gcode.New(429, "Too Many Requests", "Too Many Requests")
|
|
StatusInternalServerError = gcode.New(500, "Internal Server Error", "Internal Server Error")
|
|
)
|
|
|