exceptions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. 自定义异常类和全局状态码定义
  3. """
  4. from typing import Optional
  5. class BaseAPIException(Exception):
  6. """基础API异常类"""
  7. def __init__(self, status_code: int, detail: str, code: Optional[int] = None):
  8. self.status_code = status_code
  9. self.detail = detail
  10. self.code = code or status_code
  11. super().__init__(self.detail)
  12. class BusinessException(BaseAPIException):
  13. """业务异常"""
  14. def __init__(self, detail: str, status_code: int = 400, code: Optional[int] = None):
  15. super().__init__(status_code, detail, code)
  16. class UnauthorizedException(BaseAPIException):
  17. """未授权异常"""
  18. def __init__(self, detail: str = "未经过系统授权", code: Optional[int] = None):
  19. super().__init__(401, detail, code or 401)
  20. class ForbiddenException(BaseAPIException):
  21. """权限不足异常"""
  22. def __init__(self, detail: str = "无权限访问", code: Optional[int] = None):
  23. super().__init__(403, detail, code or 403)
  24. class NotFoundException(BaseAPIException):
  25. """资源不存在异常"""
  26. def __init__(self, detail: str = "资源不存在", code: Optional[int] = None):
  27. super().__init__(404, detail, code or 404)
  28. class ValidationException(BaseAPIException):
  29. """参数验证异常"""
  30. def __init__(self, detail: str = "参数验证失败", code: Optional[int] = None):
  31. super().__init__(400, detail, code or 400)
  32. class InternalServerException(BaseAPIException):
  33. """服务器内部错误异常"""
  34. def __init__(self, detail: str = "服务器内部错误", code: Optional[int] = None):
  35. super().__init__(500, detail, code or 500)
  36. # 全局状态码定义
  37. class StatusCode:
  38. """全局状态码定义"""
  39. # 成功
  40. SUCCESS = 200
  41. # 客户端错误
  42. BAD_REQUEST = 400 # 请求参数错误
  43. UNAUTHORIZED = 401 # 未授权
  44. FORBIDDEN = 403 # 权限不足
  45. NOT_FOUND = 404 # 资源不存在
  46. METHOD_NOT_ALLOWED = 405 # 方法不允许
  47. NOT_ACCEPTABLE = 406 # 请求头不支持
  48. REQUEST_TIMEOUT = 408 # 请求超时
  49. CONFLICT = 409 # 资源冲突
  50. PAYLOAD_TOO_LARGE = 413 # 文件过大
  51. # 服务器错误
  52. INTERNAL_SERVER_ERROR = 500 # 服务器内部错误
  53. BAD_GATEWAY = 502 # 网关错误
  54. SERVICE_UNAVAILABLE = 503 # 服务不可用
  55. GATEWAY_TIMEOUT = 504 # 网关超时
  56. # 业务错误码(自定义)
  57. INVALID_TOKEN = 600 # 无效的登录信息
  58. TOKEN_EXPIRED = 601 # Token过期
  59. # 全局错误消息定义
  60. class ErrorMessage:
  61. """全局错误消息定义"""
  62. # 认证相关
  63. UNAUTHORIZED = "未经过系统授权"
  64. INVALID_TOKEN = "无效的登录信息"
  65. TOKEN_EXPIRED = "当前登录已过期,请重新登录"
  66. # 权限相关
  67. FORBIDDEN = "无权限访问"
  68. NO_OWNER_PERMISSION = "无该货主权限!"
  69. # 资源相关
  70. NOT_FOUND = "资源不存在"
  71. FILE_NOT_FOUND = "文件不存在"
  72. # 参数相关
  73. MISSING_PARAM = "缺少必要参数"
  74. INVALID_PARAM = "参数验证失败"
  75. INVALID_HEADER = "请求的头部不支持"
  76. # 业务相关
  77. BUCKET_CONFIG_FAILED = "获取桶配置失败"
  78. UPLOAD_FAILED = "上传文件失败"
  79. DOWNLOAD_FAILED = "下载文件失败"
  80. DELETE_FAILED = "删除失败"
  81. CREATE_FOLDER_FAILED = "创建文件夹失败"
  82. LIST_FILES_FAILED = "列出文件失败"
  83. # 系统相关
  84. INTERNAL_ERROR = "服务器内部错误"
  85. REQUEST_FAILED = "请求失败"
  86. INVALID_RESPONSE = "无效的JSON响应"