my_exceptions.py 758 B

123456789101112131415161718192021
  1. from rest_framework.views import exception_handler
  2. from rest_framework.response import Response
  3. from django.db import DatabaseError
  4. def custom_exception_handler(exc, context):
  5. # Call REST framework's default exception handler first,
  6. # to get the standard error response.
  7. response = exception_handler(exc, context)
  8. # Now add the HTTP status code to the response.
  9. if response is not None:
  10. response.data['status_code'] = response.status_code
  11. response = Response(response.data)
  12. else:
  13. if isinstance(exc, DatabaseError):
  14. pass
  15. # response = Response({'detail': 'Database Error'})
  16. else:
  17. pass
  18. # response = Response({'detail': 'Other Error'})
  19. return response