refactor: 后端代码质量优化 - 复用公共库、使用标准库、类型安全错误判断
## 高优先级修复 - stats_service_impl: 使用 strings.SplitN 替代错误的索引分割 - provider_handler: 使用 errors.Is(err, gorm.ErrDuplicatedKey) 替代字符串匹配 - client: 重写 isNetworkError 使用 errors.As/Is 类型安全判断 - proxy_handler: 使用 encoding/json 标准库解析 JSON(extractModelName、isStreamRequest) ## 中优先级修复 - stats_handler: 添加 parseDateParam 辅助函数消除重复日期解析 - pkg/errors: 新增 ErrRequestCreate/Send/ResponseRead 错误类型和 WithCause 方法 - client: 使用结构化错误替代 fmt.Errorf - ConversionEngine: logger 依赖注入,替换所有 zap.L() 调用 ## 低优先级修复 - encoder: 删除 joinStrings,使用 strings.Join - adapter: 删除 modelInfoRegex 正则,使用 isModelInfoPath 字符串函数 ## 文档更新 - README.md: 添加公共库使用指南和编码规范章节 - specs: 同步 delta specs 到 main specs(error-handling、structured-logging、request-validation) ## 归档 - openspec/changes/archive/2026-04-20-refactor-backend-code-quality/
This commit is contained in:
@@ -27,6 +27,17 @@ func (e *AppError) Unwrap() error {
|
||||
return e.Cause
|
||||
}
|
||||
|
||||
// WithCause returns a copy of the error with the given cause
|
||||
func (e *AppError) WithCause(cause error) *AppError {
|
||||
return &AppError{
|
||||
Code: e.Code,
|
||||
Message: e.Message,
|
||||
HTTPStatus: e.HTTPStatus,
|
||||
Cause: cause,
|
||||
Context: e.Context,
|
||||
}
|
||||
}
|
||||
|
||||
// NewAppError creates a new AppError
|
||||
func NewAppError(code, message string, httpStatus int) *AppError {
|
||||
return &AppError{
|
||||
@@ -46,6 +57,9 @@ var (
|
||||
ErrInternal = NewAppError("internal_error", "内部错误", http.StatusInternalServerError)
|
||||
ErrDatabaseNotInit = NewAppError("database_not_initialized", "数据库未初始化", http.StatusInternalServerError)
|
||||
ErrConflict = NewAppError("conflict", "资源已存在", http.StatusConflict)
|
||||
ErrRequestCreate = NewAppError("request_create_error", "创建请求失败", http.StatusInternalServerError)
|
||||
ErrRequestSend = NewAppError("request_send_error", "发送请求失败", http.StatusBadGateway)
|
||||
ErrResponseRead = NewAppError("response_read_error", "读取响应失败", http.StatusBadGateway)
|
||||
)
|
||||
|
||||
// AsAppError 尝试将 error 转换为 *AppError
|
||||
|
||||
@@ -90,6 +90,9 @@ func TestPredefinedErrors(t *testing.T) {
|
||||
{"ErrInternal", ErrInternal, "internal_error", http.StatusInternalServerError},
|
||||
{"ErrDatabaseNotInit", ErrDatabaseNotInit, "database_not_initialized", http.StatusInternalServerError},
|
||||
{"ErrConflict", ErrConflict, "conflict", http.StatusConflict},
|
||||
{"ErrRequestCreate", ErrRequestCreate, "request_create_error", http.StatusInternalServerError},
|
||||
{"ErrRequestSend", ErrRequestSend, "request_send_error", http.StatusBadGateway},
|
||||
{"ErrResponseRead", ErrResponseRead, "response_read_error", http.StatusBadGateway},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -123,3 +126,16 @@ func TestAsAppError(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWithCause(t *testing.T) {
|
||||
cause := errors.New("连接超时")
|
||||
err := ErrRequestSend.WithCause(cause)
|
||||
assert.Equal(t, "request_send_error", err.Code)
|
||||
assert.Equal(t, http.StatusBadGateway, err.HTTPStatus)
|
||||
assert.Equal(t, cause, err.Cause)
|
||||
assert.True(t, errors.Is(err, cause))
|
||||
|
||||
var appErr *AppError
|
||||
assert.True(t, errors.As(err, &appErr))
|
||||
assert.Equal(t, "request_send_error", appErr.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user