biz_exception.py 1016 B

12345678910111213141516171819202122232425262728293031
  1. from typing import Optional, Dict, Any
  2. class BizException(Exception):
  3. def __init__(
  4. self,
  5. code: int,
  6. message: str,
  7. http_status: int = 400,
  8. extra: Optional[Dict[str, Any]] = None,
  9. ):
  10. self.code = code
  11. self.message = message
  12. self.http_status = http_status
  13. self.extra = extra
  14. class NotFoundError(BizException):
  15. def __init__(self, message="Resource not found"):
  16. super().__init__(code=40401, message=message, http_status=404)
  17. class PermissionDeniedError(BizException):
  18. def __init__(self, message="Permission denied"):
  19. super().__init__(code=40301, message=message, http_status=403)
  20. class SessionExpiredOrInvalid(BizException):
  21. def __init__(self, message="Session expired or invalid"):
  22. super().__init__(code=40101, message=message, http_status=401)
  23. class BizLogicError(BizException):
  24. def __init__(self, message="Business logic error"):
  25. super().__init__(code=40001, message=message)