refactor: 移除全局单例、修复分层违规、统一错误与响应
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrotskin/internal/errors"
|
||||
apperrors "carrotskin/internal/errors"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/internal/types"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -190,31 +191,31 @@ func RespondWithError(c *gin.Context, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// 使用errors.Is检查预定义错误
|
||||
if errors.Is(err, errors.ErrUserNotFound) ||
|
||||
errors.Is(err, errors.ErrProfileNotFound) ||
|
||||
errors.Is(err, errors.ErrTextureNotFound) ||
|
||||
errors.Is(err, errors.ErrNotFound) {
|
||||
// 使用标准库 errors.Is 检查预定义错误
|
||||
if errors.Is(err, apperrors.ErrUserNotFound) ||
|
||||
errors.Is(err, apperrors.ErrProfileNotFound) ||
|
||||
errors.Is(err, apperrors.ErrTextureNotFound) ||
|
||||
errors.Is(err, apperrors.ErrNotFound) {
|
||||
RespondNotFound(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if errors.Is(err, errors.ErrProfileNoPermission) ||
|
||||
errors.Is(err, errors.ErrTextureNoPermission) ||
|
||||
errors.Is(err, errors.ErrForbidden) {
|
||||
if errors.Is(err, apperrors.ErrProfileNoPermission) ||
|
||||
errors.Is(err, apperrors.ErrTextureNoPermission) ||
|
||||
errors.Is(err, apperrors.ErrForbidden) {
|
||||
RespondForbidden(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if errors.Is(err, errors.ErrUnauthorized) ||
|
||||
errors.Is(err, errors.ErrInvalidToken) ||
|
||||
errors.Is(err, errors.ErrTokenExpired) {
|
||||
if errors.Is(err, apperrors.ErrUnauthorized) ||
|
||||
errors.Is(err, apperrors.ErrInvalidToken) ||
|
||||
errors.Is(err, apperrors.ErrTokenExpired) {
|
||||
RespondUnauthorized(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 检查AppError类型
|
||||
var appErr *errors.AppError
|
||||
var appErr *apperrors.AppError
|
||||
if errors.As(err, &appErr) {
|
||||
c.JSON(appErr.Code, model.NewErrorResponse(
|
||||
appErr.Code,
|
||||
|
||||
Reference in New Issue
Block a user