fix(middleware): 修复panic恢复处理逻辑
- 统一错误响应结构,使用response.Error进行处理 - 添加字符串类型错误的处理分支 - 为未知错误类型设置默认错误消息 - 确保所有panic情况都会调用End方法结束响应 - 保持原有日志记录功能main v1.0.00015
parent
53aa8367dc
commit
edff2f198a
|
|
@ -12,12 +12,16 @@ func ErrorHandler() gin.HandlerFunc {
|
|||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
res := response.Error(c).SetCode(http.StatusInternalServerError)
|
||||
switch e := err.(type) {
|
||||
case string:
|
||||
res = res.SetMsg(e)
|
||||
case error:
|
||||
response.Error(c).SetCode(http.StatusInternalServerError).SetMsg(e.Error())
|
||||
res = res.SetMsg(e.Error())
|
||||
default:
|
||||
log.Error(c.Errors.Last().Error())
|
||||
res = res.SetMsg("服务器内部异常,请稍后重试")
|
||||
}
|
||||
res.End()
|
||||
log.Error("发生panic=》", "path:", c.Request.URL.Path, ",method:", c.Request.Method, ",", err)
|
||||
c.Abort()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue