feat(backend): 添加核心架构模块
- 添加认证模块 (auth.py) - 添加 CRUD 基础操作 (crud.py) - 添加异常处理 (exceptions.py) - 添加日志模块 (logging.py) - 添加响应格式 (response.py) - 添加依赖注入 (dependencies.py) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
119
backend/app/core/exceptions.py
Normal file
119
backend/app/core/exceptions.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Custom Exceptions
|
||||
自定义异常类
|
||||
"""
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
class AppException(Exception):
|
||||
"""基础应用异常"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str,
|
||||
code: str = "INTERNAL_ERROR",
|
||||
status_code: int = 500,
|
||||
details: Optional[dict] = None
|
||||
):
|
||||
self.message = message
|
||||
self.code = code
|
||||
self.status_code = status_code
|
||||
self.details = details
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class NotFoundException(AppException):
|
||||
"""资源未找到异常"""
|
||||
|
||||
def __init__(self, resource: str, resource_id: Any = None):
|
||||
message = f"{resource} not found"
|
||||
if resource_id:
|
||||
message = f"{resource} with id '{resource_id}' not found"
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="NOT_FOUND",
|
||||
status_code=404
|
||||
)
|
||||
|
||||
|
||||
class ValidationException(AppException):
|
||||
"""验证异常"""
|
||||
|
||||
def __init__(self, message: str, field: str = None, details: dict = None):
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="VALIDATION_ERROR",
|
||||
status_code=422,
|
||||
details={"field": field, **(details or {})}
|
||||
)
|
||||
|
||||
|
||||
class DuplicateException(AppException):
|
||||
"""重复资源异常"""
|
||||
|
||||
def __init__(self, resource: str, field: str = None):
|
||||
message = f"{resource} already exists"
|
||||
if field:
|
||||
message = f"{resource} with {field} already exists"
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="DUPLICATE",
|
||||
status_code=409
|
||||
)
|
||||
|
||||
|
||||
class UnauthorizedException(AppException):
|
||||
"""未授权异常"""
|
||||
|
||||
def __init__(self, message: str = "Unauthorized"):
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="UNAUTHORIZED",
|
||||
status_code=401
|
||||
)
|
||||
|
||||
|
||||
class ForbiddenException(AppException):
|
||||
"""禁止访问异常"""
|
||||
|
||||
def __init__(self, message: str = "Forbidden"):
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="FORBIDDEN",
|
||||
status_code=403
|
||||
)
|
||||
|
||||
|
||||
class RateLimitException(AppException):
|
||||
"""速率限制异常"""
|
||||
|
||||
def __init__(self, message: str = "Rate limit exceeded"):
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="RATE_LIMIT",
|
||||
status_code=429
|
||||
)
|
||||
|
||||
|
||||
class FileProcessingException(AppException):
|
||||
"""文件处理异常"""
|
||||
|
||||
def __init__(self, message: str, file_name: str = None):
|
||||
details = {"file_name": file_name} if file_name else None
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="FILE_PROCESSING_ERROR",
|
||||
status_code=422,
|
||||
details=details
|
||||
)
|
||||
|
||||
|
||||
class DatabaseException(AppException):
|
||||
"""数据库异常"""
|
||||
|
||||
def __init__(self, message: str = "Database operation failed"):
|
||||
super().__init__(
|
||||
message=message,
|
||||
code="DATABASE_ERROR",
|
||||
status_code=500
|
||||
)
|
||||
Reference in New Issue
Block a user