|
| 1 | +# 国际化错误处理示例 |
| 2 | + |
| 3 | +本文档展示如何在Controller层处理Repository返回的国际化错误。 |
| 4 | + |
| 5 | +## 在Controller中处理Repository错误 |
| 6 | + |
| 7 | +### 方法1:直接处理RepositoryError |
| 8 | + |
| 9 | +```go |
| 10 | +func (ac *AuthController) Login(c *gin.Context) { |
| 11 | + var u model.Admin |
| 12 | + if err := c.ShouldBindJSON(&u); err != nil { |
| 13 | + response.ParamError(c, err.Error()) |
| 14 | + return |
| 15 | + } |
| 16 | + |
| 17 | + admin, err := ac.AdminRepository.Login(u) |
| 18 | + if err != nil { |
| 19 | + // 检查是否为RepositoryError |
| 20 | + if repoErr, ok := err.(*common.RepositoryError); ok { |
| 21 | + // 获取本地化错误消息 |
| 22 | + localizedMsg := repoErr.GetLocalizedMessage(c) |
| 23 | + |
| 24 | + // 根据错误类型返回不同的HTTP状态码 |
| 25 | + switch repoErr.Code { |
| 26 | + case "user_not_found", "password_incorrect": |
| 27 | + response.PasswordIncorrect(c, localizedMsg) |
| 28 | + case "user_disabled", "user_role_disabled": |
| 29 | + response.Forbidden(c, localizedMsg) |
| 30 | + case "user_not_logged_in": |
| 31 | + response.Unauthorized(c, localizedMsg) |
| 32 | + default: |
| 33 | + response.InternalServerError(c, localizedMsg) |
| 34 | + } |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + // 处理其他类型的错误 |
| 39 | + response.InternalServerError(c, err.Error()) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // 成功处理... |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### 方法2:创建通用错误处理函数 |
| 48 | + |
| 49 | +```go |
| 50 | +// 在controller包中创建通用错误处理函数 |
| 51 | +func HandleRepositoryError(c *gin.Context, err error) bool { |
| 52 | + if err == nil { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + // 检查是否为RepositoryError |
| 57 | + if repoErr, ok := err.(*common.RepositoryError); ok { |
| 58 | + localizedMsg := repoErr.GetLocalizedMessage(c) |
| 59 | + |
| 60 | + switch repoErr.Code { |
| 61 | + case "user_not_found", "password_incorrect": |
| 62 | + response.PasswordIncorrect(c, localizedMsg) |
| 63 | + case "user_disabled", "user_role_disabled": |
| 64 | + response.Forbidden(c, localizedMsg) |
| 65 | + case "user_not_logged_in": |
| 66 | + response.Unauthorized(c, localizedMsg) |
| 67 | + case "no_users_found", "role_info_failed", "no_users_with_role": |
| 68 | + response.NotFound(c, localizedMsg) |
| 69 | + default: |
| 70 | + response.InternalServerError(c, localizedMsg) |
| 71 | + } |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + // 处理其他类型的错误 |
| 76 | + response.InternalServerError(c, err.Error()) |
| 77 | + return true |
| 78 | +} |
| 79 | + |
| 80 | +// 在Controller中使用 |
| 81 | +func (ac *AuthController) Login(c *gin.Context) { |
| 82 | + var u model.Admin |
| 83 | + if err := c.ShouldBindJSON(&u); err != nil { |
| 84 | + response.ParamError(c, err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + admin, err := ac.AdminRepository.Login(u) |
| 89 | + if HandleRepositoryError(c, err) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // 成功处理... |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## 支持的错误类型 |
| 98 | + |
| 99 | +| 错误代码 | 中文消息 | 英文消息 | 建议HTTP状态码 | |
| 100 | +|---------|---------|---------|---------------| |
| 101 | +| `user_not_found` | 用户不存在 | User not found | 401 | |
| 102 | +| `user_disabled` | 用户被禁用 | User is disabled | 403 | |
| 103 | +| `user_role_disabled` | 用户角色被禁用 | User role is disabled | 403 | |
| 104 | +| `password_incorrect` | 密码错误 | Incorrect password | 401 | |
| 105 | +| `user_not_logged_in` | 用户未登录 | User not logged in | 401 | |
| 106 | +| `user_not_found_by_id` | 未获取到ID为%d的用户 | User not found by ID %d | 404 | |
| 107 | +| `no_users_found` | 未获取到任何用户信息 | No user information found | 404 | |
| 108 | +| `role_info_failed` | 根据角色ID获取角色信息失败 | Failed to get role information by role ID | 500 | |
| 109 | +| `no_users_with_role` | 根据角色ID未获取到拥有该角色的用户 | No users with this role found by role ID | 404 | |
| 110 | + |
| 111 | +## 语言检测 |
| 112 | + |
| 113 | +系统通过以下方式检测用户语言偏好: |
| 114 | + |
| 115 | +1. 检查Gin Context中的`lang`字段 |
| 116 | +2. 如果未设置,默认使用中文(`zh`) |
| 117 | +3. 支持的语言:`zh`(中文)、`en`(英文) |
| 118 | + |
| 119 | +## 性能考虑 |
| 120 | + |
| 121 | +- 错误消息本地化性能:约20ns/op |
| 122 | +- 错误对象创建性能:约0.2ns/op |
| 123 | +- 建议在高频调用场景中缓存错误对象 |
0 commit comments