""" 自定义异常类和全局状态码定义 """ from typing import Optional class BaseAPIException(Exception): """基础API异常类""" def __init__(self, status_code: int, detail: str, code: Optional[int] = None): self.status_code = status_code self.detail = detail self.code = code or status_code super().__init__(self.detail) class BusinessException(BaseAPIException): """业务异常""" def __init__(self, detail: str, status_code: int = 400, code: Optional[int] = None): super().__init__(status_code, detail, code) class UnauthorizedException(BaseAPIException): """未授权异常""" def __init__(self, detail: str = "未经过系统授权", code: Optional[int] = None): super().__init__(401, detail, code or 401) class ForbiddenException(BaseAPIException): """权限不足异常""" def __init__(self, detail: str = "无权限访问", code: Optional[int] = None): super().__init__(403, detail, code or 403) class NotFoundException(BaseAPIException): """资源不存在异常""" def __init__(self, detail: str = "资源不存在", code: Optional[int] = None): super().__init__(404, detail, code or 404) class ValidationException(BaseAPIException): """参数验证异常""" def __init__(self, detail: str = "参数验证失败", code: Optional[int] = None): super().__init__(400, detail, code or 400) class InternalServerException(BaseAPIException): """服务器内部错误异常""" def __init__(self, detail: str = "服务器内部错误", code: Optional[int] = None): super().__init__(500, detail, code or 500) # 全局状态码定义 class StatusCode: """全局状态码定义""" # 成功 SUCCESS = 200 # 客户端错误 BAD_REQUEST = 400 # 请求参数错误 UNAUTHORIZED = 401 # 未授权 FORBIDDEN = 403 # 权限不足 NOT_FOUND = 404 # 资源不存在 METHOD_NOT_ALLOWED = 405 # 方法不允许 NOT_ACCEPTABLE = 406 # 请求头不支持 REQUEST_TIMEOUT = 408 # 请求超时 CONFLICT = 409 # 资源冲突 PAYLOAD_TOO_LARGE = 413 # 文件过大 # 服务器错误 INTERNAL_SERVER_ERROR = 500 # 服务器内部错误 BAD_GATEWAY = 502 # 网关错误 SERVICE_UNAVAILABLE = 503 # 服务不可用 GATEWAY_TIMEOUT = 504 # 网关超时 # 业务错误码(自定义) INVALID_TOKEN = 600 # 无效的登录信息 TOKEN_EXPIRED = 601 # Token过期 # 全局错误消息定义 class ErrorMessage: """全局错误消息定义""" # 认证相关 UNAUTHORIZED = "未经过系统授权" INVALID_TOKEN = "无效的登录信息" TOKEN_EXPIRED = "当前登录已过期,请重新登录" # 权限相关 FORBIDDEN = "无权限访问" NO_OWNER_PERMISSION = "无该货主权限!" # 资源相关 NOT_FOUND = "资源不存在" FILE_NOT_FOUND = "文件不存在" # 参数相关 MISSING_PARAM = "缺少必要参数" INVALID_PARAM = "参数验证失败" INVALID_HEADER = "请求的头部不支持" # 业务相关 BUCKET_CONFIG_FAILED = "获取桶配置失败" UPLOAD_FAILED = "上传文件失败" DOWNLOAD_FAILED = "下载文件失败" DELETE_FAILED = "删除失败" CREATE_FOLDER_FAILED = "创建文件夹失败" LIST_FILES_FAILED = "列出文件失败" # 系统相关 INTERNAL_ERROR = "服务器内部错误" REQUEST_FAILED = "请求失败" INVALID_RESPONSE = "无效的JSON响应"