exceptions.py 998 B

123456789101112131415161718192021222324252627282930313233
  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. return super().__init__(f'pdns response code: {response.status_code}, pdns response body: {response.text}')
  19. class ConcurrencyException(APIException):
  20. status_code = status.HTTP_429_TOO_MANY_REQUESTS
  21. default_detail = 'Too many concurrent requests.'
  22. default_code = 'concurrency_conflict'