/* @Time : 2019-11-07 14:55 @Author : zr */ package middleware import ( "github.com/gin-gonic/gin" "net/http" "video_course/errors" "video_course/global" ) type ResponseBase struct { Code int Memo string } func ErrorResponse() gin.HandlerFunc { return func(c *gin.Context) { defer func() { if len(c.Errors) > 0 { stdErr := errors.FromError(c.Errors[0].Err) var resp interface{} resp1 := &ResponseBase{ Code: int(stdErr.Code), Memo: errors.ErrorCode(stdErr.Code).ShowMsg(), } resp = resp1 if global.Project.Debug { var resp2 struct { ResponseBase DebugMsg string } resp2.ResponseBase = *resp1 resp2.DebugMsg = stdErr.Error() resp = resp2 } c.JSON(http.StatusOK, resp) } }() c.Next() } }