MCPcopy Index your code
hub / github.com/encode/django-rest-framework / exception_handler

Function exception_handler

rest_framework/views.py:72–102  ·  view source on GitHub ↗

Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's built-in `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be rais

(exc, context)

Source from the content-addressed store, hash-verified

70
71
72def exception_handler(exc, context):
73 """
74 Returns the response that should be used for any given exception.
75
76 By default we handle the REST framework `APIException`, and also
77 Django's built-in `Http404` and `PermissionDenied` exceptions.
78
79 Any unhandled exceptions may return `None`, which will cause a 500 error
80 to be raised.
81 """
82 if isinstance(exc, Http404):
83 exc = exceptions.NotFound(*(exc.args))
84 elif isinstance(exc, PermissionDenied):
85 exc = exceptions.PermissionDenied(*(exc.args))
86
87 if isinstance(exc, exceptions.APIException):
88 headers = {}
89 if getattr(exc, 'auth_header', None):
90 headers['WWW-Authenticate'] = exc.auth_header
91 if getattr(exc, 'wait', None):
92 headers['Retry-After'] = '%d' % exc.wait
93
94 if isinstance(exc.detail, (list, dict)):
95 data = exc.detail
96 else:
97 data = {'detail': exc.detail}
98
99 set_rollback()
100 return Response(data, status=exc.status_code, headers=headers)
101
102 return None
103
104
105class APIView(View):

Callers 1

handle_exceptionMethod · 0.85

Calls 2

ResponseClass · 0.90
set_rollbackFunction · 0.85

Tested by

no test coverage detected