fix(middleware): 修复panic恢复中的错误处理逻辑
- 当发生panic时检查上下文错误列表 - 使用最后一个错误作为响应内容 - 移除直接使用panic值的逻辑 - 改进错误消息的安全性,避免敏感信息泄露 - 确保在panic情况下正确终止请求处理main v1.0.00013
parent
60dcd37901
commit
3bfca3805a
|
|
@ -12,8 +12,14 @@ 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 {
|
||||||
|
// 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)
|
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()
|
c.Abort()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue