64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
"""业务异常与鉴权错误码(Wave 0 AppError + 风控模块 ApiError 并存)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
class AppError(Exception):
|
|
def __init__(
|
|
self,
|
|
code: int,
|
|
message: str,
|
|
*,
|
|
http_status: int = 400,
|
|
error_code: str | None = None,
|
|
audit_event: str | None = None,
|
|
) -> None:
|
|
super().__init__(message)
|
|
self.code = code
|
|
self.message = message
|
|
self.http_status = http_status
|
|
self.error_code = error_code
|
|
self.audit_event = audit_event
|
|
|
|
|
|
class UnauthorizedError(AppError):
|
|
def __init__(self, message: str = "未授权", *, error_code: str = "AUTH_401") -> None:
|
|
super().__init__(401, message, http_status=401, error_code=error_code, audit_event="auth_denied")
|
|
|
|
|
|
class ForbiddenError(AppError):
|
|
def __init__(
|
|
self,
|
|
message: str = "无权访问",
|
|
*,
|
|
error_code: str = "AUTH_403",
|
|
audit_event: str = "auth_denied",
|
|
) -> None:
|
|
super().__init__(403, message, http_status=403, error_code=error_code, audit_event=audit_event)
|
|
|
|
|
|
class NotFoundError(LookupError):
|
|
"""资源不存在(HTTP 404)。"""
|
|
|
|
|
|
class PermissionDenied(PermissionError):
|
|
"""归属/权限拒绝(HTTP 403);code 对齐 JWT 手册 §6.1 错误码。"""
|
|
|
|
def __init__(self, code: str, message: str) -> None:
|
|
super().__init__(message)
|
|
self.code = code
|
|
|
|
|
|
class StateConflict(ValueError):
|
|
"""状态机冲突(HTTP 409;如处置非 pending_review 的预警单)。"""
|
|
|
|
|
|
class ApiError(Exception):
|
|
"""业务 HTTP 错误(统一错误体出口 · 手册 §10)。"""
|
|
|
|
def __init__(self, status_code: int, error_code: str, message: str) -> None:
|
|
super().__init__(message)
|
|
self.status_code = status_code
|
|
self.error_code = error_code
|
|
self.message = message
|