Skip to content

Commit 21358d1

Browse files
committed
<fix>[errorcode]: fix compile bug in replaceSystemError and add mandatory contract rules
- Fix isRetryable() method name (was getRetryable() - method does not exist) - Fix httpStatus null check on primitive int (use != 0 instead) - Add section 6 to error-display-contract.md with 3 mandatory rules from frontend team
1 parent 97efa7c commit 21358d1

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

core/src/main/java/org/zstack/core/errorcode/ErrorFacadeImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ private void replaceSystemError(ErrorCodeList err, String details) {
8686
if (subErr.getLocalizedMessage() != null) {
8787
err.setLocalizedMessage(subErr.getLocalizedMessage());
8888
}
89-
if (subErr.getRetryable() != null) {
90-
err.setRetryable(subErr.getRetryable());
89+
if (subErr.isRetryable()) {
90+
err.setRetryable(subErr.isRetryable());
9191
}
92-
if (subErr.getHttpStatus() != null) {
92+
if (subErr.getHttpStatus() != 0) {
9393
err.setHttpStatus(subErr.getHttpStatus());
9494
}
9595
} catch (Exception e) {

docs/error-display-contract.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,38 @@ if (Boolean.TRUE.equals(error.getRetryable())) {
111111
| `IO_ERROR` | 提示存储或网络 I/O 异常,检查物理环境。 |
112112
| `HTTP_ERROR` | 处理底层的网络通信错误。 |
113113
| `CANCEL_ERROR` | 默默处理或展示操作已取消的轻提示。 |
114+
115+
116+
## 6. 硬性契约 — 不可违反 (Mandatory Contract Rules)
117+
118+
> ⚠️ 以下三条规则为前端团队与后端的硬性约定,任何变更必须经过前后端双方 code review 确认。违反将导致前端返工。
119+
120+
### 规则 1: message/error 字段必须与响应状态一致
121+
122+
- **失败响应**`error` 字段 **必须** 包含完整的 ErrorCode 对象(含 `code``description``details` 等)
123+
- **成功响应**`error` 字段 **必须**`null`**绝对禁止** 出现任何错误相关信息
124+
- **灰色地带禁令**:不允许出现"成功但携带警告/失败原因"的混合态。如需传递警告,必须使用独立的 `warnings` 字段(当前不存在,需另行设计)
125+
126+
```
127+
// ✅ 正确:失败响应
128+
{"success": false, "error": {"code": "SYS.1001", "description": "...", ...}}
129+
130+
// ✅ 正确:成功响应
131+
{"success": true, "error": null}
132+
133+
// ❌ 禁止:成功但带错误信息
134+
{"success": true, "error": {"code": "SYS.1001", ...}}
135+
```
136+
137+
### 规则 2: 成功态绝对禁止 message
138+
139+
- 成功响应(`success=true`)中,**不得** 出现 `message``errorMessage``failReason` 或任何语义上表示"失败原因"的字段
140+
- `localizedMessage` 字段 **** 存在于 ErrorCode 对象内部,成功响应中 ErrorCode 对象为 null,因此 `localizedMessage` 自然不会出现
141+
- 如果业务需要在成功响应中携带提示信息,**必须** 使用明确区分于错误的字段名(如 `notice``tip`),且需单独设计、单独评审
142+
143+
### 规则 3: globalErrorCode 的唯一归属层级 — API 级
144+
145+
- `globalErrorCode`**API 级** 的全局错误标识符,由 `ErrorFacadeImpl` 统一注册和分配
146+
- **禁止** 在 action 级、task 级或 UI 组件级重新定义或覆盖 `globalErrorCode` 的含义
147+
- 前端 **** 通过 API 响应中的 `globalErrorCode` 进行国际化映射,不会也不应该从其他层级获取此字段
148+
- 任何新增 `globalErrorCode` 必须在 `ErrorFacadeImpl` 中注册,并同步更新 i18n 资源文件

0 commit comments

Comments
 (0)