feat: Phase 0 — monorepo 骨架 + 全表迁移 + FastAPI/Next 骨架 + CI
- 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
This commit is contained in:
44
packages/shared/ww_shared/errors.py
Normal file
44
packages/shared/ww_shared/errors.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""统一错误码与领域异常(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)
|
||||
Reference in New Issue
Block a user