A multipurpose python flask API server and administration SPA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

  1. """Error definitions for Corvus."""
  2. from typing import Dict
  3. from corvus.api.decorators import return_json
  4. from corvus.api.model import APIResponse
  5. class BaseError(Exception):
  6. """Corvus Base Error Class (5xx errors)."""
  7. def __init__(
  8. self,
  9. message: str = 'Unknown error',
  10. status_code: int = 500,
  11. extra_fields: Dict[str, str] = None) -> None:
  12. """Populate The Error Definition."""
  13. super().__init__(message)
  14. self.message = message
  15. self.status_code = status_code
  16. self.extra_fields = extra_fields
  17. def to_dict(self) -> dict:
  18. """Serialize an error message to return."""
  19. return {
  20. 'message': self.message,
  21. 'status_code': self.status_code
  22. }
  23. class ClientError(BaseError):
  24. """Corvus errors where the client is wrong (4xx errors)."""
  25. def __init__(self,
  26. message: str = 'Unknown client error',
  27. status_code: int = 400,
  28. extra_fields: Dict[str, str] = None) -> None:
  29. """Init for client originated errors."""
  30. super().__init__(message, status_code, extra_fields)
  31. class ValidationError(ClientError):
  32. """Corvus Validation Error."""
  33. pass
  34. @return_json
  35. def handle_corvus_base_error(error: BaseError) -> APIResponse:
  36. """Error handler for basic Corvus raised errors."""
  37. return APIResponse(payload=error, status=error.status_code)