package middleware import ( "fmt" "net/http" "runtime" "strconv" "strings" "time" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/config" ) const ERROR_MSG = "网络异常,请稍后重试" func CustomError(c *gin.Context) { defer func() { if err := recover(); err != nil { if c.IsAborted() { c.Status(200) } msg := ERROR_MSG switch errStr := err.(type) { case string: p := strings.Split(errStr, "#") if len(p) == 3 && p[0] == "CustomError" { statusCode, e := strconv.Atoi(p[1]) if e != nil { break } c.Status(statusCode) fmt.Println( time.Now().Format("2006-01-02 15:04:05"), "[ERROR]", c.Request.Method, c.Request.URL, statusCode, c.Request.RequestURI, c.ClientIP(), p[2], ) if config.ApplicationConfig.Mode == "dev" { msg = p[2] } c.JSON(http.StatusOK, gin.H{ "code": statusCode, "msg": msg, }) } else { if config.ApplicationConfig.Mode == "dev" { msg = errStr } c.JSON(http.StatusOK, gin.H{ "code": 500, "msg": msg, }) } case runtime.Error: if config.ApplicationConfig.Mode == "dev" { msg = errStr.Error() } c.JSON(http.StatusOK, gin.H{ "code": 500, "msg": msg, }) default: panic(err) } } }() c.Next() }