Files
servicebase/pkg/middleware/global_panic.go
2025-11-19 14:24:13 +08:00

37 lines
513 B
Go

package middleware
import (
"net/http"
"runtime/debug"
"github.com/gin-gonic/gin"
)
func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
c.JSON(http.StatusOK, gin.H{
"code": 500,
"message": "系统异常",
"detailError": errorToString(r),
"data": nil,
})
c.Abort()
}
}()
c.Next()
}
func errorToString(r any) string {
switch v := r.(type) {
case error:
return v.Error()
default:
return r.(string)
}
}