156 lines
4.2 KiB
Python
156 lines
4.2 KiB
Python
"""
|
|
自定义异常类
|
|
"""
|
|
from typing import Any, Dict, Optional
|
|
from fastapi import HTTPException, status
|
|
|
|
|
|
class BusinessException(Exception):
|
|
"""业务逻辑异常基类"""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str,
|
|
code: int = status.HTTP_400_BAD_REQUEST,
|
|
error_code: Optional[str] = None,
|
|
data: Optional[Dict[str, Any]] = None
|
|
):
|
|
"""
|
|
初始化业务异常
|
|
|
|
Args:
|
|
message: 错误消息
|
|
code: HTTP状态码
|
|
error_code: 业务错误码
|
|
data: 附加数据
|
|
"""
|
|
self.message = message
|
|
self.code = code
|
|
self.error_code = error_code
|
|
self.data = data
|
|
super().__init__(self.message)
|
|
|
|
|
|
class NotFoundException(BusinessException):
|
|
"""资源不存在异常"""
|
|
|
|
def __init__(self, resource: str = "资源"):
|
|
super().__init__(
|
|
message=f"{resource}不存在",
|
|
code=status.HTTP_404_NOT_FOUND,
|
|
error_code="RESOURCE_NOT_FOUND"
|
|
)
|
|
|
|
|
|
class AlreadyExistsException(BusinessException):
|
|
"""资源已存在异常"""
|
|
|
|
def __init__(self, resource: str = "资源"):
|
|
super().__init__(
|
|
message=f"{resource}已存在",
|
|
code=status.HTTP_409_CONFLICT,
|
|
error_code="RESOURCE_ALREADY_EXISTS"
|
|
)
|
|
|
|
|
|
class PermissionDeniedException(BusinessException):
|
|
"""权限不足异常"""
|
|
|
|
def __init__(self, message: str = "权限不足"):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_403_FORBIDDEN,
|
|
error_code="PERMISSION_DENIED"
|
|
)
|
|
|
|
|
|
class AuthenticationFailedException(BusinessException):
|
|
"""认证失败异常"""
|
|
|
|
def __init__(self, message: str = "认证失败"):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_401_UNAUTHORIZED,
|
|
error_code="AUTHENTICATION_FAILED"
|
|
)
|
|
|
|
|
|
class ValidationFailedException(BusinessException):
|
|
"""验证失败异常"""
|
|
|
|
def __init__(self, message: str = "数据验证失败", errors: Optional[Dict] = None):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
error_code="VALIDATION_FAILED",
|
|
data=errors
|
|
)
|
|
|
|
|
|
class InvalidCredentialsException(AuthenticationFailedException):
|
|
"""无效凭据异常"""
|
|
|
|
def __init__(self, message: str = "用户名或密码错误"):
|
|
super().__init__(message)
|
|
self.error_code = "INVALID_CREDENTIALS"
|
|
|
|
|
|
class TokenExpiredException(AuthenticationFailedException):
|
|
"""令牌过期异常"""
|
|
|
|
def __init__(self, message: str = "令牌已过期,请重新登录"):
|
|
super().__init__(message)
|
|
self.error_code = "TOKEN_EXPIRED"
|
|
|
|
|
|
class InvalidTokenException(AuthenticationFailedException):
|
|
"""无效令牌异常"""
|
|
|
|
def __init__(self, message: str = "无效的令牌"):
|
|
super().__init__(message)
|
|
self.error_code = "INVALID_TOKEN"
|
|
|
|
|
|
class CaptchaException(BusinessException):
|
|
"""验证码异常"""
|
|
|
|
def __init__(self, message: str = "验证码错误"):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_400_BAD_REQUEST,
|
|
error_code="CAPTCHA_ERROR"
|
|
)
|
|
|
|
|
|
class UserLockedException(BusinessException):
|
|
"""用户被锁定异常"""
|
|
|
|
def __init__(self, message: str = "用户已被锁定,请联系管理员"):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_403_FORBIDDEN,
|
|
error_code="USER_LOCKED"
|
|
)
|
|
|
|
|
|
class UserDisabledException(BusinessException):
|
|
"""用户被禁用异常"""
|
|
|
|
def __init__(self, message: str = "用户已被禁用"):
|
|
super().__init__(
|
|
message=message,
|
|
code=status.HTTP_403_FORBIDDEN,
|
|
error_code="USER_DISABLED"
|
|
)
|
|
|
|
|
|
class StateTransitionException(BusinessException):
|
|
"""状态转换异常"""
|
|
|
|
def __init__(self, current_state: str, target_state: str):
|
|
super().__init__(
|
|
message=f"无法从状态 '{current_state}' 转换到 '{target_state}'",
|
|
code=status.HTTP_400_BAD_REQUEST,
|
|
error_code="INVALID_STATE_TRANSITION"
|
|
)
|