fix(middleware): 修复panic恢复处理逻辑

- 移除冗余的错误数量检查
- 使用类型断言处理不同类型的panic值
- 改进错误消息处理和日志记录
- 简化了异常恢复流程
main v1.0.00014
black1552 2026-02-03 16:36:33 +08:00
parent 3bfca3805a
commit 53aa8367dc
1 changed files with 5 additions and 6 deletions

View File

@ -12,12 +12,11 @@ func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
if len(c.Errors) > 0 { switch e := err.(type) {
// Step3: Use the last error case error:
laErr := c.Errors.Last().Err response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(e.Error())
default:
// Step4: Respond with a generic error message log.Error(c.Errors.Last().Error())
response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(laErr.Error()).End()
} }
log.Error("发生panic=》", "path", c.Request.URL.Path, ",method:", c.Request.Method, ",", err) log.Error("发生panic=》", "path", c.Request.URL.Path, ",method:", c.Request.Method, ",", err)
c.Abort() c.Abort()