- Added `auth.py` for mock login and JWT issuance. - Introduced `chat.py` for handling chat requests with role-based access control. - Enhanced `main.py` to include new routers and middleware for tracing. - Implemented input validation in `input_guard.py` to prevent SQL injection. - Created repositories for managing agent sessions and audit logs. - Added exception handling for authorization errors. - Updated settings to include JWT configuration. - Introduced tests for authentication and input validation.
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
"""统一业务异常。"""
|
|
|
|
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)
|