exceptions.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from rest_framework import status
  2. from rest_framework.exceptions import APIException
  3. __all__ = (
  4. 'AbortRequest',
  5. 'AbortScript',
  6. 'AbortTransaction',
  7. 'PermissionsViolation',
  8. 'PreconditionFailed',
  9. 'RQWorkerNotRunningException',
  10. )
  11. class AbortTransaction(Exception):
  12. """
  13. A dummy exception used to trigger a database transaction rollback.
  14. """
  15. pass
  16. class AbortRequest(Exception):
  17. """
  18. Raised to cleanly abort a request (for example, by a pre_save signal receiver).
  19. """
  20. def __init__(self, message):
  21. self.message = message
  22. class AbortScript(Exception):
  23. """
  24. Raised to cleanly abort a script.
  25. """
  26. pass
  27. class PermissionsViolation(Exception):
  28. """
  29. Raised when an operation was prevented because it would violate the
  30. allowed permissions.
  31. """
  32. message = "Operation failed due to object-level permissions violation"
  33. class PreconditionFailed(APIException):
  34. """
  35. Raised when an If-Match precondition is not satisfied (HTTP 412).
  36. Optionally carries the current ETag so it can be included in the response.
  37. """
  38. status_code = status.HTTP_412_PRECONDITION_FAILED
  39. default_detail = 'Precondition failed.'
  40. default_code = 'precondition_failed'
  41. def __init__(self, detail=None, code=None, etag=None):
  42. super().__init__(detail=detail, code=code)
  43. self.etag = etag
  44. class RQWorkerNotRunningException(APIException):
  45. """
  46. Indicates the temporary inability to enqueue a new task (e.g. custom script execution) because no RQ worker
  47. processes are currently running.
  48. """
  49. status_code = status.HTTP_503_SERVICE_UNAVAILABLE
  50. default_detail = 'Unable to process request: RQ worker process not running.'
  51. default_code = 'rq_worker_not_running'