- uv(workspace) + pnpm monorepo;docker-compose(pg+api+web) - SQLAlchemy 16 MVP 表 + Alembic 初版迁移(无漂移,users stub) - FastAPI 骨架:统一错误信封(带 request_id) + structlog + /jobs/:id + OpenAPI - Next.js 骨架:纸感主题 token + OpenAPI→TS 客户端代码生成(gen:api) - CI(ruff/mypy/pytest + pg service + alembic 漂移校验) - 四份设计规格(PRODUCT/UX/ARCHITECTURE/DEV_PLAN) + CLAUDE.md
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""统一错误码与领域异常(ARCH §7.1)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from enum import StrEnum
|
||
|
||
|
||
class ErrorCode(StrEnum):
|
||
NOT_FOUND = "NOT_FOUND"
|
||
VALIDATION = "VALIDATION"
|
||
CONFLICT_UNRESOLVED = "CONFLICT_UNRESOLVED" # 未决冲突禁验收
|
||
LLM_UNAVAILABLE = "LLM_UNAVAILABLE" # 回退耗尽
|
||
RATE_LIMITED = "RATE_LIMITED"
|
||
INTERNAL = "INTERNAL"
|
||
|
||
|
||
# 错误码 -> HTTP 状态
|
||
HTTP_STATUS: dict[ErrorCode, int] = {
|
||
ErrorCode.NOT_FOUND: 404,
|
||
ErrorCode.VALIDATION: 422,
|
||
ErrorCode.CONFLICT_UNRESOLVED: 409,
|
||
ErrorCode.LLM_UNAVAILABLE: 503,
|
||
ErrorCode.RATE_LIMITED: 429,
|
||
ErrorCode.INTERNAL: 500,
|
||
}
|
||
|
||
|
||
class AppError(Exception):
|
||
"""领域异常:在边界处映射为统一错误信封。"""
|
||
|
||
def __init__(
|
||
self,
|
||
code: ErrorCode,
|
||
message: str,
|
||
details: dict[str, object] | None = None,
|
||
) -> None:
|
||
super().__init__(message)
|
||
self.code = code
|
||
self.message = message
|
||
self.details = details or {}
|
||
|
||
@property
|
||
def http_status(self) -> int:
|
||
return HTTP_STATUS.get(self.code, 500)
|