diff --git a/middleware/middleware.go b/middleware/middleware.go index 0cc9ab2..6e1ad15 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -12,8 +12,14 @@ func ErrorHandler() gin.HandlerFunc { return func(c *gin.Context) { defer func() { if err := recover(); err != nil { + if len(c.Errors) > 0 { + // Step3: Use the last error + laErr := c.Errors.Last().Err + + // Step4: Respond with a generic error message + response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(laErr.Error()).End() + } log.Error("发生panic=》", "path:", c.Request.URL.Path, ",method:", c.Request.Method, ",", err) - response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(err.(error).Error()).End() c.Abort() } }()