refactor(backend): 重构后端
This commit is contained in:
1 parent
c8054476c1
commit
bba6e6f221
32 files changed
+252
-296
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# 错误处理规范
|
||||
|
||||
> 更新时间:2026-06-22
|
||||
> 更新时间:2026-06-17
|
||||
> 适用范围:HXOPS 前后端 API 与 UI 错误反馈
|
||||
> 读者:组内同事、AI 编程助手
|
||||
> 文档定位:**错误契约、分层职责与禁止项**的权威说明。`AGENTS.md` §8 仅保留强约束摘要,细则以本文为准。
|
||||
@@ -40,7 +40,7 @@
|
||||
|
||||
| 字段 | 说明 |
|
||||
|------|------|
|
||||
| `error.code` | 机器可读错误码,格式 `DOMAIN.SUBTYPE`(如 `AUTH.UNAUTHORIZED`) |
|
||||
| `error.code` | 机器可读错误码;常规 API 用 `DOMAIN.SUBTYPE`(如 `AUTH.UNAUTHORIZED`);企微登录等历史域可保留 `WECOM_*` 扁平码 |
|
||||
| `error.message` | 英文或简短说明,供日志与 fallback;**前端优先用 i18n 映射** |
|
||||
| `error.details` | 可选结构化信息(字段名、冲突项等),供前端分支逻辑 |
|
||||
| `error.retryable` | 是否建议用户重试(503 等) |
|
||||
@@ -58,7 +58,7 @@
|
||||
| **Service(`services/`)** | 业务校验失败 `raise AppError` 子类;不构造 HTTP Response |
|
||||
| **全局 handler(`app/__init__.py`)** | 捕获 `AppError` / `HTTPException` / 未预期异常 → `error_payload` |
|
||||
|
||||
### 2.2 使用 `app/errors.py`
|
||||
### 2.2 使用 `app.core.errors`
|
||||
|
||||
优先使用已有子类,语义不明确时再 `raise AppError(...)`:
|
||||
|
||||
@@ -74,7 +74,7 @@
|
||||
**示例(service / route):**
|
||||
|
||||
```python
|
||||
from app.errors import ValidationError, NotFoundError, ConflictError
|
||||
from app.core.errors import ValidationError, NotFoundError, ConflictError, AppError
|
||||
|
||||
if not name:
|
||||
raise ValidationError("Name is required")
|
||||
@@ -82,9 +82,11 @@ if not row:
|
||||
raise NotFoundError("Agent not found")
|
||||
if email_exists:
|
||||
raise ConflictError("Email already exists", code="USER.EMAIL_EXISTS", details={"field": "email"})
|
||||
# 企微等业务码:code 字段即机器码,禁止再塞 details.error_code
|
||||
raise AppError(code="WECOM_NOT_IN_AGENT_SCOPE", message="...", http_status=401)
|
||||
```
|
||||
|
||||
**成功响应:** 使用 `app.response_utils.ok(data)`,或在 envelope 开启时返回 plain dict(由 `@app.after_request` 包装)。
|
||||
**成功响应:** 使用 `app.core.response.ok(data)`,或在 `ENABLE_API_ENVELOPE=true` 时返回 plain dict(由 `@app.after_request` 包装)。
|
||||
|
||||
### 2.3 错误码命名
|
||||
|
||||
@@ -101,8 +103,22 @@ if email_exists:
|
||||
| 在 service 里 `return (jsonify(...), 4xx)` | `raise AppError` 交给 handler |
|
||||
| 500 时把堆栈/ SQL 返回给客户端 | 日志记录细节;客户端仅 `SYSTEM.INTERNAL_ERROR` |
|
||||
| 随意字符串作 code(`"not_found"`) | `RESOURCE.NOT_FOUND` 等规范 code |
|
||||
| 用 `details.error_code` 传递主错误码 | 主码写在 `AppError.code`;`details` 只放结构化字段 |
|
||||
|
||||
> 历史路由(如部分 `workspace_shipping_notice.py`)仍有 legacy `{"error": "..."}` 写法;**新接口与改动处须改为 `AppError`**,逐步收敛。
|
||||
### 2.5 Dify Tool API 例外(刻意不走 Envelope)
|
||||
|
||||
供 Dify Chatflow / RPA 调用的 Tool 蓝图在 Blueprint 级注册 `@bp.errorhandler(AppError)`,返回:
|
||||
|
||||
```json
|
||||
{ "ok": false, "error_code": "missing_auth", "message": "..." }
|
||||
```
|
||||
|
||||
| 蓝图 | 前缀 |
|
||||
|------|------|
|
||||
| `api/file_check_tools.py` | `/api/file-check/tools/*` |
|
||||
| `api/shipping_notice_tools.py` | `/api/shipping-notice/tools/*` |
|
||||
|
||||
**禁止**将上述 Tool 路由改为标准 envelope;契约测试见 `backend/tests/test_api_error_contract.py`。
|
||||
|
||||
---
|
||||
|
||||
@@ -185,7 +201,7 @@ catch (e) {
|
||||
|
||||
## 4. 新增错误码检查清单
|
||||
|
||||
1. 后端:在 `app/errors.py` 选用或新增语义明确的 `code`。
|
||||
1. 后端:在 `app.core.errors` 选用或新增语义明确的 `code`。
|
||||
2. 前端:在 `locales/{zh-CN,zh-TW,en,th}/errors.json` 的 `codes` 下增加键(`USER.EMAIL_EXISTS` → `USER_EMAIL_EXISTS`)。
|
||||
3. 若需按 `details` 分支,在 composable 文档注释中说明字段约定。
|
||||
4. 自测:触发该错误,确认四语言文案与 HTTP 状态码正确。
|
||||
@@ -196,9 +212,10 @@ catch (e) {
|
||||
|
||||
| 文件 | 说明 |
|
||||
|------|------|
|
||||
| `backend/app/errors.py` | 业务异常类型 |
|
||||
| `backend/app/response_utils.py` | `ok()`、`error_payload()` |
|
||||
| `backend/app/core/errors.py` | 业务异常类型 |
|
||||
| `backend/app/core/response.py` | `ok()`、`error_payload()` |
|
||||
| `backend/app/__init__.py` | 全局 errorhandler、envelope 包装 |
|
||||
| `backend/tests/test_api_error_contract.py` | 标准 envelope + Tool API 契约冒烟 |
|
||||
| `frontend/src/shared/api/client.ts` | `requestJson`、`BusinessError` |
|
||||
| `frontend/src/locales/*/errors.json` | 错误码 i18n |
|
||||
| `frontend/src/shared/composables/useMessage.ts` | 全局 error/success 弹窗 |
|
||||
|
||||
Reference in new issue
Block a user