P0-01: Add ESCAPE clause to LIKE queries in operation_log.go and device.go P0-02: Add atomic Increment to L1Cache and L2Cache interfaces P0-07: Add TOTP verification step after password login P1-01: Sanitize error messages in error.go middleware P1-03: Remove err.Error() from export error messages P1-04: Add error return to CountByResultSince in login_log.go P1-05: Add transactional DeleteCascade to RoleRepository P1-06: Add PasswordChangedAt tracking for JWT token invalidation P1-07: Wrap theme SetDefault in database transaction P1-08: Use config values for database pool parameters P1-09: Add rows.Err() checks in social_account_repo.go P1-10: Validate sortOrder with map in user.go ORDER BY P1-11: Add GORM tags to Announcement struct P1-15: Add pageSize upper limit (100) to device and log handlers
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
apierrors "github.com/user-management-system/internal/pkg/errors"
|
|
)
|
|
|
|
// ErrorHandler 错误处理中间件
|
|
func ErrorHandler() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
|
|
// 检查是否有错误
|
|
if len(c.Errors) > 0 {
|
|
// 获取最后一个错误
|
|
err := c.Errors.Last()
|
|
|
|
// 判断错误类型
|
|
if appErr, ok := err.Err.(*apierrors.ApplicationError); ok {
|
|
c.JSON(int(appErr.Code), appErr)
|
|
} else {
|
|
// 安全修复:未知错误不泄露内部详情,只返回通用消息
|
|
// 详细错误记录到日志,供调试使用
|
|
c.JSON(http.StatusInternalServerError, apierrors.New(http.StatusInternalServerError, "", "服务器内部错误"))
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// Recover 恢复中间件
|
|
func Recover() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, apierrors.New(http.StatusInternalServerError, "", "服务器内部错误"))
|
|
c.Abort()
|
|
}
|
|
}()
|
|
c.Next()
|
|
}
|
|
}
|