2026-09-07 17:20:42 +08:00
|
|
|
"""统一业务异常。"""
|
|
|
|
|
|
|
|
|
|
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)
|