refactor: align framework cleanup with new bootstrap flow
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
@@ -43,10 +43,6 @@ const (
|
||||
CodeServiceUnavailable = 2004 // 服务不可用
|
||||
CodeTimeout = 2005 // 请求超时
|
||||
CodeTaskQueueError = 2006 // 任务队列错误
|
||||
|
||||
// 向后兼容的别名(供现有代码使用)
|
||||
CodeBadRequest = CodeInvalidParam // 别名:参数验证失败
|
||||
CodeAuthServiceUnavailable = CodeServiceUnavailable // 别名:认证服务不可用
|
||||
)
|
||||
|
||||
// errorMessages 错误消息映射表(中文)
|
||||
|
||||
@@ -15,10 +15,9 @@ var (
|
||||
|
||||
// AppError 表示带错误码的应用错误
|
||||
type AppError struct {
|
||||
Code int // 应用错误码
|
||||
Message string // 错误消息
|
||||
HTTPStatus int // HTTP 状态码(自动从 Code 映射,可通过 WithHTTPStatus 覆盖)
|
||||
Err error // 底层错误(可选)
|
||||
Code int // 应用错误码
|
||||
Message string // 错误消息
|
||||
Err error // 底层错误(可选)
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
@@ -39,9 +38,8 @@ func New(code int, message string) *AppError {
|
||||
message = GetMessage(code, "zh-CN")
|
||||
}
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
HTTPStatus: GetHTTPStatus(code), // 自动从错误码映射 HTTP 状态码
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,15 +50,8 @@ func Wrap(code int, message string, err error) *AppError {
|
||||
message = GetMessage(code, "zh-CN")
|
||||
}
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
HTTPStatus: GetHTTPStatus(code), // 自动从错误码映射 HTTP 状态码
|
||||
Err: err,
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPStatus 设置自定义 HTTP 状态码(用于特殊场景)
|
||||
func (e *AppError) WithHTTPStatus(status int) *AppError {
|
||||
e.HTTPStatus = status
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func handleError(c *fiber.Ctx, err error, logger *zap.Logger) error {
|
||||
// 应用自定义错误
|
||||
code = e.Code
|
||||
message = e.Message
|
||||
httpStatus = e.HTTPStatus
|
||||
httpStatus = GetHTTPStatus(e.Code)
|
||||
|
||||
// 记录错误日志(包含完整上下文)
|
||||
logFields := append(errCtx.ToLogFields(),
|
||||
|
||||
@@ -87,32 +87,22 @@ func TestSafeErrorHandler(t *testing.T) {
|
||||
// TestAppErrorMethods 测试 AppError 的方法
|
||||
func TestAppErrorMethods(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err *AppError
|
||||
expectedError string
|
||||
expectedHTTPStatus int
|
||||
expectedCode int
|
||||
name string
|
||||
err *AppError
|
||||
expectedError string
|
||||
expectedCode int
|
||||
}{
|
||||
{
|
||||
name: "基本 AppError",
|
||||
err: New(CodeInvalidParam, "参数错误"),
|
||||
expectedError: "参数错误",
|
||||
expectedHTTPStatus: 400,
|
||||
expectedCode: CodeInvalidParam,
|
||||
name: "基本 AppError",
|
||||
err: New(CodeInvalidParam, "参数错误"),
|
||||
expectedError: "参数错误",
|
||||
expectedCode: CodeInvalidParam,
|
||||
},
|
||||
{
|
||||
name: "带自定义 HTTP 状态码",
|
||||
err: New(CodeNotFound, "用户不存在").WithHTTPStatus(404),
|
||||
expectedError: "用户不存在",
|
||||
expectedHTTPStatus: 404,
|
||||
expectedCode: CodeNotFound,
|
||||
},
|
||||
{
|
||||
name: "空消息使用默认",
|
||||
err: New(CodeDatabaseError, ""),
|
||||
expectedError: "数据库错误",
|
||||
expectedHTTPStatus: 500,
|
||||
expectedCode: CodeDatabaseError,
|
||||
name: "空消息使用默认",
|
||||
err: New(CodeDatabaseError, ""),
|
||||
expectedError: "数据库错误",
|
||||
expectedCode: CodeDatabaseError,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -127,11 +117,6 @@ func TestAppErrorMethods(t *testing.T) {
|
||||
if tt.err.Code != tt.expectedCode {
|
||||
t.Errorf("Code = %d, expected %d", tt.err.Code, tt.expectedCode)
|
||||
}
|
||||
|
||||
// 测试 HTTPStatus 字段
|
||||
if tt.err.HTTPStatus != tt.expectedHTTPStatus {
|
||||
t.Errorf("HTTPStatus = %d, expected %d", tt.err.HTTPStatus, tt.expectedHTTPStatus)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user