fix(middleware): 修复panic恢复中的错误处理逻辑

- 当发生panic时检查上下文错误列表
- 使用最后一个错误作为响应内容
- 移除直接使用panic值的逻辑
- 改进错误消息的安全性,避免敏感信息泄露
- 确保在panic情况下正确终止请求处理
main v1.0.00013
black1552 2026-02-03 16:25:36 +08:00
parent 60dcd37901
commit 3bfca3805a
1 changed files with 7 additions and 1 deletions

View File

@ -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()
}
}()