package response import ( "github.com/gin-gonic/gin" "gogs.tyduyong.com/duyong/dy-pkg/errors" "gogs.tyduyong.com/duyong/dy-pkg/logs" "net/http" ) type ErrResponse struct { // Code defines the business error code. Code int `json:"code"` // Message contains the detail of this message. // This message is suitable to be exposed to external Message string `json:"message"` // Reference returns the reference document which maybe useful to solve this error. Reference string `json:"reference,omitempty"` } // WriteResponse 如果错误是来自我们自己的错误包。返回时会解析并打印错误日志 func WriteResponse(c *gin.Context, err error, data interface{}) { if err != nil { logs.Errorf("%#+v", err) coder := errors.ParseCoder(err) c.JSON(coder.HTTPStatus(), ErrResponse{ Code: coder.Code(), Message: coder.String(), Reference: coder.Reference(), }) return } c.JSON(http.StatusOK, data) }