From 53aa8367dcc933201f8b015e72550fbe797e8d70 Mon Sep 17 00:00:00 2001 From: black1552 Date: Tue, 3 Feb 2026 16:36:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(middleware):=20=E4=BF=AE=E5=A4=8Dpanic?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除冗余的错误数量检查 - 使用类型断言处理不同类型的panic值 - 改进错误消息处理和日志记录 - 简化了异常恢复流程 --- middleware/middleware.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/middleware/middleware.go b/middleware/middleware.go index 6e1ad15..f59c788 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -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()