exceptions.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import json
  2. from rest_framework import status
  3. from rest_framework.exceptions import APIException, ValidationError
  4. class RequestEntityTooLarge(APIException):
  5. status_code = status.HTTP_413_REQUEST_ENTITY_TOO_LARGE
  6. default_detail = 'Payload too large.'
  7. default_code = 'too_large'
  8. class PDNSValidationError(ValidationError):
  9. pdns_code = 422
  10. def __init__(self, response=None):
  11. try:
  12. detail = json.loads(response.text)['error']
  13. except json.JSONDecodeError:
  14. detail = response.text
  15. return super().__init__(detail={'detail': detail}, code='invalid')
  16. class PDNSException(APIException):
  17. def __init__(self, response=None):
  18. self.response = response
  19. detail = f'pdns response code: {response.status_code}, body: {response.text}' if response is not None else None
  20. return super().__init__(detail)
  21. class ConcurrencyException(APIException):
  22. status_code = status.HTTP_429_TOO_MANY_REQUESTS
  23. default_detail = 'Too many concurrent requests.'
  24. default_code = 'concurrency_conflict'