Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from rest_framework import exceptions, status
- from rest_framework.response import Response
- from rest_framework.views import exception_handler, set_rollback
- from rest_framework.exceptions import (
- ErrorDetail,
- AuthenticationFailed,
- APIException,
- PermissionDenied,
- NotFound
- )
- class ExceptionHandler:
- def get_domain(self):
- """
- Domain examples
- """
- if self.status_code >= 500:
- return 'data_process'
- return 'request'
- def get_full_details(self, detail):
- if isinstance(detail, list):
- return [self.get_full_details(item) for item in detail]
- elif isinstance(detail, dict):
- return {
- 'domain': self.get_domain(),
- 'state': {
- key: self.get_full_details(value)
- for key, value in detail.items()
- }
- }
- return {
- 'message': str(detail),
- 'reason': detail.code
- }
- def get_exception_message(self):
- status_code = self.status_code
- exception_string = _('Bad request')
- # TODO: refactor to a map with more options of exception messages
- if status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR:
- exception_string = APIException.default_detail
- elif status_code == status.HTTP_401_UNAUTHORIZED:
- exception_string = AuthenticationFailed.default_detail
- elif status_code == status.HTTP_403_FORBIDDEN:
- exception_string = PermissionDenied.default_detail
- elif status_code == status.HTTP_404_NOT_FOUND:
- exception_string = NotFound.default_detail
- return exception_string
- def __call__(self, exc, context):
- # Call REST framework's default exception handler first,
- # to get the standard error response.
- response = exception_handler(exc, context)
- self.status_code = response.status_code
- if response is not None and hasattr(exc, 'detail'):
- response.data = {
- 'code': self.status_code,
- 'message': self.get_exception_message()
- }
- full_details = self.get_full_details(exc.detail)
- if isinstance(full_details, dict):
- errors = [{
- 'message': exc.default_detail,
- 'domain': self.get_domain(),
- 'reason': exc.default_code,
- **full_details
- }]
- else:
- errors = full_details
- response.data['errors'] = errors
- return response
- custom_exception_handler3 = ExceptionHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement