errors.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 全局业务异常类
  5. 业务代码执行异常时,可以使用 raise xxxError 触发内部错误,它尽可能实现带有后台任务的异常,但它不适用于**自定义响应状态码**
  6. 如果要求使用**自定义响应状态码**,则可以通过 return await response_base.fail(res=CustomResponseCode.xxx) 直接返回
  7. """ # noqa: E501
  8. from typing import Any
  9. from fastapi import HTTPException
  10. from starlette.background import BackgroundTask
  11. from common.response.response_code import CustomErrorCode, StandardResponseCode
  12. class BaseExceptionMixin(Exception):
  13. code: int
  14. def __init__(self, *, msg: str = None, data: Any = None, background: BackgroundTask | None = None):
  15. self.msg = msg
  16. self.data = data
  17. # The original background task: https://www.starlette.io/background/
  18. self.background = background
  19. class HTTPError(HTTPException):
  20. def __init__(self, *, code: int, msg: Any = None, headers: dict[str, Any] | None = None):
  21. super().__init__(status_code=code, detail=msg, headers=headers)
  22. class CustomError(BaseExceptionMixin):
  23. def __init__(self, *, error: CustomErrorCode, data: Any = None, background: BackgroundTask | None = None):
  24. self.code = error.code
  25. super().__init__(msg=error.msg, data=data, background=background)
  26. class RequestError(BaseExceptionMixin):
  27. code = StandardResponseCode.HTTP_400
  28. def __init__(self, *, msg: str = 'Bad Request', data: Any = None, background: BackgroundTask | None = None):
  29. super().__init__(msg=msg, data=data, background=background)
  30. class ForbiddenError(BaseExceptionMixin):
  31. code = StandardResponseCode.HTTP_403
  32. def __init__(self, *, msg: str = 'Forbidden', data: Any = None, background: BackgroundTask | None = None):
  33. super().__init__(msg=msg, data=data, background=background)
  34. class NotFoundError(BaseExceptionMixin):
  35. code = StandardResponseCode.HTTP_404
  36. def __init__(self, *, msg: str = 'Not Found', data: Any = None, background: BackgroundTask | None = None):
  37. super().__init__(msg=msg, data=data, background=background)
  38. class ServerError(BaseExceptionMixin):
  39. code = StandardResponseCode.HTTP_500
  40. def __init__(
  41. self, *, msg: str = 'Internal Server Error', data: Any = None, background: BackgroundTask | None = None
  42. ):
  43. super().__init__(msg=msg, data=data, background=background)
  44. class GatewayError(BaseExceptionMixin):
  45. code = StandardResponseCode.HTTP_502
  46. def __init__(self, *, msg: str = 'Bad Gateway', data: Any = None, background: BackgroundTask | None = None):
  47. super().__init__(msg=msg, data=data, background=background)
  48. class AuthorizationError(BaseExceptionMixin):
  49. code = StandardResponseCode.HTTP_401
  50. def __init__(self, *, msg: str = 'Permission Denied', data: Any = None, background: BackgroundTask | None = None):
  51. super().__init__(msg=msg, data=data, background=background)
  52. class TokenError(HTTPError):
  53. code = StandardResponseCode.HTTP_401
  54. def __init__(self, *, msg: str = 'Not Authenticated', headers: dict[str, Any] | None = None):
  55. super().__init__(code=self.code, msg=msg, headers=headers or {'WWW-Authenticate': 'Bearer'})