## 主要修复内容 ### 🔧 导入错误修复 - 修复 updateCategoryLogic.go 缺失的导入 (errorx, model, sql, time) - 修复 loginLogic.go 中 errors 包应为 errorx 包的问题 - 修复 uploadPhotoLogic.go 中错误处理不统一的问题 - 修复 photo 查询相关文件缺失 model 包导入 ### ⚡ 错误处理统一化 - 统一使用项目自定义的 errorx 包替代标准库 errors - 完善 model.ErrNotFound 错误判断逻辑 - 添加详细的错误日志记录 - 统一响应代码使用 errorx.Success ### 🆕 错误代码扩展 - 新增 UserDisabled (1003) 错误代码 - 新增 InvalidParameter (400) 错误代码别名 - 完善错误代码到 HTTP 状态码的映射 - 修复重复错误代码导致的编译问题 ### ✅ 代码质量保证 - 解决所有编译错误,确保 go build 成功 - 修复 15 个后端逻辑文件的导入问题 - 整理 go.mod 依赖包 - 更新项目任务进度文档 ## 影响的文件 - backend/internal/logic/auth/loginLogic.go - backend/internal/logic/category/updateCategoryLogic.go - backend/internal/logic/photo/uploadPhotoLogic.go - backend/internal/logic/photo/getPhotoLogic.go - backend/internal/logic/photo/getPhotoListLogic.go - backend/pkg/errorx/errorx.go - TASK_PROGRESS.md
99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package errorx
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
)
|
||
|
||
const (
|
||
// 通用错误代码
|
||
Success = 0
|
||
ServerError = 500
|
||
ParamError = 400
|
||
AuthError = 401
|
||
NotFound = 404
|
||
Forbidden = 403
|
||
InvalidParameter = 400 // 与 ParamError 统一
|
||
|
||
// 业务错误代码
|
||
UserNotFound = 1001
|
||
UserExists = 1002
|
||
UserDisabled = 1003
|
||
InvalidPassword = 1004
|
||
TokenExpired = 1005
|
||
TokenInvalid = 1006
|
||
|
||
PhotoNotFound = 2001
|
||
PhotoUploadFail = 2002
|
||
|
||
CategoryNotFound = 3001
|
||
CategoryExists = 3002
|
||
)
|
||
|
||
var codeText = map[int]string{
|
||
Success: "Success",
|
||
ServerError: "Server Error",
|
||
ParamError: "Parameter Error", // ParamError 和 InvalidParameter 都映射到这里
|
||
AuthError: "Authentication Error",
|
||
NotFound: "Not Found",
|
||
Forbidden: "Forbidden",
|
||
|
||
UserNotFound: "User Not Found",
|
||
UserExists: "User Already Exists",
|
||
UserDisabled: "User Disabled",
|
||
InvalidPassword: "Invalid Password",
|
||
TokenExpired: "Token Expired",
|
||
TokenInvalid: "Token Invalid",
|
||
|
||
PhotoNotFound: "Photo Not Found",
|
||
PhotoUploadFail: "Photo Upload Failed",
|
||
|
||
CategoryNotFound: "Category Not Found",
|
||
CategoryExists: "Category Already Exists",
|
||
}
|
||
|
||
type CodeError struct {
|
||
Code int `json:"code"`
|
||
Msg string `json:"msg"`
|
||
}
|
||
|
||
func (e *CodeError) Error() string {
|
||
return fmt.Sprintf("Code: %d, Msg: %s", e.Code, e.Msg)
|
||
}
|
||
|
||
func New(code int, msg string) *CodeError {
|
||
return &CodeError{
|
||
Code: code,
|
||
Msg: msg,
|
||
}
|
||
}
|
||
|
||
func NewWithCode(code int) *CodeError {
|
||
msg, ok := codeText[code]
|
||
if !ok {
|
||
msg = codeText[ServerError]
|
||
}
|
||
return &CodeError{
|
||
Code: code,
|
||
Msg: msg,
|
||
}
|
||
}
|
||
|
||
func GetHttpStatus(code int) int {
|
||
switch code {
|
||
case Success:
|
||
return http.StatusOK
|
||
case ParamError: // ParamError 和 InvalidParameter 都是 400,所以只需要一个 case
|
||
return http.StatusBadRequest
|
||
case AuthError, TokenExpired, TokenInvalid:
|
||
return http.StatusUnauthorized
|
||
case NotFound, UserNotFound, PhotoNotFound, CategoryNotFound:
|
||
return http.StatusNotFound
|
||
case Forbidden, UserDisabled:
|
||
return http.StatusForbidden
|
||
case UserExists, CategoryExists:
|
||
return http.StatusConflict
|
||
default:
|
||
return http.StatusInternalServerError
|
||
}
|
||
} |