This commit is contained in:
oneao committed 2025-11-01 17:29:25 +08:00
1 parent b116690769
commit 1d6bb2b7f5
23 files changed
+922 -116

No files matched your search

@@ -0,0 +1,31 @@
package middleware
import (
"base-go-v2/internal/logx"
response "base-go-v2/internal/reponse"
"base-go-v2/internal/router"
"runtime/debug"
)
func RecoveryMiddleware() router.HandlerFunc {
return func(c *router.Context) error {
defer func() {
if r := recover(); r != nil {
stack := string(debug.Stack()) // 获取堆栈
logx.Logger.Error("请求 panic",
logx.String("path", string(c.RequestCtx.Path())),
logx.String("method", string(c.RequestCtx.Method())),
logx.String("panic", r.(error).Error()),
logx.String("stack", stack),
)
// 返回统一 JSON 响应
_ = response.Error(c).Message("系统 Panic").Send()
}
}()
// 调用后续 handler
return c.Next()
}
}