21 lines
543 B
Go
21 lines
543 B
Go
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"ego/internal/serializer"
|
||
|
"ego/pkg/logger"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
// UnifiedErrorResponse 统一错误处理中间件
|
||
|
func UnifiedErrorResponse() gin.HandlerFunc {
|
||
|
return gin.CustomRecovery(func(c *gin.Context, err any) {
|
||
|
// 1. 记录完整错误日志
|
||
|
logger.Error(c, fmt.Sprintf("系统错误: %v", err))
|
||
|
// 2. 返回统一的Json风格
|
||
|
c.AbortWithStatusJSON(http.StatusOK, serializer.Err(http.StatusInternalServerError, "系统繁忙稍后重试!", err.(error)))
|
||
|
})
|
||
|
}
|