refactor: 统一错误消息数据源,优化错误码与映射表管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m36s

主要改动:
- 改造 errors.New() 和 Wrap() 函数签名为可变参数,优先使用 errorMessages 映射表
- 添加 allErrorCodes 注册表和 init() 启动时校验,确保错误码与映射表一致
- 添加 TestAllCodesHaveMessages 和 TestNoOrphanMessages 测试防止映射表腐化
- 清理 109 处与映射表一致的冗余硬编码(service 层)
- 保留业务特定消息覆盖能力

新增 API 用法:
- errors.New(errors.CodeUnauthorized) // 使用映射表默认消息
- errors.New(errors.CodeNotFound, "提现申请不存在") // 覆盖为自定义消息
This commit is contained in:
2026-01-22 18:27:42 +08:00
parent b68e7ec013
commit 6821e5abcf
28 changed files with 665 additions and 81 deletions

View File

@@ -43,7 +43,7 @@ func (h *PersonalCustomerHandler) SendCode(c *fiber.Ctx) error {
zap.String("phone", req.Phone),
zap.Error(err),
)
return errors.Wrap(errors.CodeInternalError, "发送验证码失败", err)
return errors.Wrap(errors.CodeInternalError, err, "发送验证码失败")
}
return response.Success(c, fiber.Map{
@@ -88,7 +88,7 @@ func (h *PersonalCustomerHandler) Login(c *fiber.Ctx) error {
zap.String("phone", req.Phone),
zap.Error(err),
)
return errors.Wrap(errors.CodeInternalError, "登录失败", err)
return errors.Wrap(errors.CodeInternalError, err, "登录失败")
}
// 构造响应
@@ -157,7 +157,7 @@ func (h *PersonalCustomerHandler) UpdateProfile(c *fiber.Ctx) error {
zap.Uint("customer_id", customerID),
zap.Error(err),
)
return errors.Wrap(errors.CodeInternalError, "更新个人资料失败", err)
return errors.Wrap(errors.CodeInternalError, err, "更新个人资料失败")
}
return response.Success(c, fiber.Map{
@@ -181,7 +181,7 @@ func (h *PersonalCustomerHandler) GetProfile(c *fiber.Ctx) error {
zap.Uint("customer_id", customerID),
zap.Error(err),
)
return errors.Wrap(errors.CodeInternalError, "获取个人资料失败", err)
return errors.Wrap(errors.CodeInternalError, err, "获取个人资料失败")
}
// 构造响应

View File

@@ -40,8 +40,8 @@ func Recover(logger *zap.Logger) fiber.Handler {
// 但由于我们在 defer 中,需要通过 c.Next() 返回错误
panicErr := errors.Wrap(
errors.CodeInternalError,
fmt.Sprintf("服务发生异常: %v", r),
fmt.Errorf("panic: %v", r),
fmt.Sprintf("服务发生异常: %v", r),
)
// 直接调用 ErrorHandler通过返回错误