fix(middleware): 修复panic恢复处理逻辑
- 移除冗余的错误数量检查 - 使用类型断言处理不同类型的panic值 - 改进错误消息处理和日志记录 - 简化了异常恢复流程main v1.0.00014
parent
3bfca3805a
commit
53aa8367dc
|
|
@ -12,12 +12,11 @@ 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()
|
||||
switch e := err.(type) {
|
||||
case error:
|
||||
response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(e.Error())
|
||||
default:
|
||||
log.Error(c.Errors.Last().Error())
|
||||
}
|
||||
log.Error("发生panic=》", "path:", c.Request.URL.Path, ",method:", c.Request.Method, ",", err)
|
||||
c.Abort()
|
||||
|
|
|
|||
Loading…
Reference in New Issue