Advertisement
OlegKl

Untitled

Jan 22nd, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. from rest_framework import exceptions, status
  2. from rest_framework.response import Response
  3. from rest_framework.views import exception_handler, set_rollback
  4. from rest_framework.exceptions import (
  5.     ErrorDetail,
  6.     AuthenticationFailed,
  7.     APIException,
  8.     PermissionDenied,
  9.     NotFound
  10. )
  11.  
  12.  
  13. class ExceptionHandler:
  14.     def get_domain(self):
  15.         """
  16.        Domain examples
  17.        """
  18.         if self.status_code >= 500:
  19.             return 'data_process'
  20.         return 'request'
  21.  
  22.     def get_full_details(self, detail):
  23.         if isinstance(detail, list):
  24.             return [self.get_full_details(item) for item in detail]
  25.         elif isinstance(detail, dict):
  26.             return {
  27.                 'domain': self.get_domain(),
  28.                 'state': {
  29.                     key: self.get_full_details(value)
  30.                     for key, value in detail.items()
  31.                 }
  32.             }
  33.         return {
  34.             'message': str(detail),
  35.             'reason': detail.code
  36.         }
  37.  
  38.     def get_exception_message(self):
  39.         status_code = self.status_code
  40.         exception_string = _('Bad request')
  41.        
  42.         # TODO: refactor to a map with more options of exception messages
  43.         if status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR:
  44.             exception_string = APIException.default_detail
  45.         elif status_code == status.HTTP_401_UNAUTHORIZED:
  46.             exception_string = AuthenticationFailed.default_detail
  47.         elif status_code == status.HTTP_403_FORBIDDEN:
  48.             exception_string = PermissionDenied.default_detail
  49.         elif status_code == status.HTTP_404_NOT_FOUND:
  50.             exception_string = NotFound.default_detail
  51.  
  52.         return exception_string
  53.  
  54.     def __call__(self, exc, context):
  55.         # Call REST framework's default exception handler first,
  56.         # to get the standard error response.
  57.         response = exception_handler(exc, context)
  58.  
  59.         self.status_code = response.status_code
  60.  
  61.         if response is not None and hasattr(exc, 'detail'):
  62.             response.data = {
  63.                 'code': self.status_code,
  64.                 'message': self.get_exception_message()
  65.             }
  66.  
  67.             full_details = self.get_full_details(exc.detail)
  68.  
  69.             if isinstance(full_details, dict):
  70.                 errors = [{
  71.                     'message': exc.default_detail,
  72.                     'domain': self.get_domain(),
  73.                     'reason': exc.default_code,
  74.                     **full_details
  75.                 }]
  76.             else:
  77.                 errors = full_details
  78.  
  79.             response.data['errors'] = errors
  80.  
  81.         return response
  82.  
  83.  
  84. custom_exception_handler3 = ExceptionHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement