mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Support customizable view EXCEPTION_HANDLER
Add `api_settings.EXCEPTION_HANDLER` to support custom error responses. Fixes #907.
This commit is contained in:
parent
916d8ab37d
commit
b5523bcc7d
|
@ -339,6 +339,20 @@ Default: `'rest_framework.views.get_view_description'`
|
||||||
|
|
||||||
## Miscellaneous settings
|
## Miscellaneous settings
|
||||||
|
|
||||||
|
#### EXCEPTION_HANDLER
|
||||||
|
|
||||||
|
A string representing the function that should be used when returning a response for any given exception. If the function returns `None`, a 500 error will be raised.
|
||||||
|
|
||||||
|
This setting can be changed to support error responses other than the default `{"detail": "Failure..."}` responses. For example, you can use it to provide API responses like `{"errors": [{"message": "Failure...", "code": ""} ...]}`.
|
||||||
|
|
||||||
|
This should be a function with the following signature:
|
||||||
|
|
||||||
|
exception_handler(exc)
|
||||||
|
|
||||||
|
* `exc`: The exception.
|
||||||
|
|
||||||
|
Default: `'rest_framework.views.exception_handler'`
|
||||||
|
|
||||||
#### FORMAT_SUFFIX_KWARG
|
#### FORMAT_SUFFIX_KWARG
|
||||||
|
|
||||||
The name of a parameter in the URL conf that may be used to provide a format suffix.
|
The name of a parameter in the URL conf that may be used to provide a format suffix.
|
||||||
|
|
|
@ -77,6 +77,9 @@ DEFAULTS = {
|
||||||
'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
|
'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
|
||||||
'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
|
'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
|
||||||
|
|
||||||
|
# Exception handling
|
||||||
|
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
'TEST_REQUEST_RENDERER_CLASSES': (
|
'TEST_REQUEST_RENDERER_CLASSES': (
|
||||||
'rest_framework.renderers.MultiPartRenderer',
|
'rest_framework.renderers.MultiPartRenderer',
|
||||||
|
@ -125,6 +128,7 @@ IMPORT_STRINGS = (
|
||||||
'DEFAULT_MODEL_SERIALIZER_CLASS',
|
'DEFAULT_MODEL_SERIALIZER_CLASS',
|
||||||
'DEFAULT_PAGINATION_SERIALIZER_CLASS',
|
'DEFAULT_PAGINATION_SERIALIZER_CLASS',
|
||||||
'DEFAULT_FILTER_BACKENDS',
|
'DEFAULT_FILTER_BACKENDS',
|
||||||
|
'EXCEPTION_HANDLER',
|
||||||
'FILTER_BACKEND',
|
'FILTER_BACKEND',
|
||||||
'TEST_REQUEST_RENDERER_CLASSES',
|
'TEST_REQUEST_RENDERER_CLASSES',
|
||||||
'UNAUTHENTICATED_USER',
|
'UNAUTHENTICATED_USER',
|
||||||
|
|
|
@ -32,6 +32,16 @@ def basic_view(request):
|
||||||
return {'method': 'PATCH', 'data': request.DATA}
|
return {'method': 'PATCH', 'data': request.DATA}
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorView(APIView):
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
raise Exception
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def error_view(request):
|
||||||
|
raise Exception
|
||||||
|
|
||||||
|
|
||||||
def sanitise_json_error(error_dict):
|
def sanitise_json_error(error_dict):
|
||||||
"""
|
"""
|
||||||
Exact contents of JSON error messages depend on the installed version
|
Exact contents of JSON error messages depend on the installed version
|
||||||
|
@ -99,3 +109,34 @@ class FunctionBasedViewIntegrationTests(TestCase):
|
||||||
}
|
}
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
self.assertEqual(sanitise_json_error(response.data), expected)
|
self.assertEqual(sanitise_json_error(response.data), expected)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCustomExceptionHandler(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
|
||||||
|
|
||||||
|
def exception_handler(exc):
|
||||||
|
return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
api_settings.EXCEPTION_HANDLER = exception_handler
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
api_settings.EXCEPTION_HANDLER = self.DEFAULT_HANDLER
|
||||||
|
|
||||||
|
def test_class_based_view_exception_handler(self):
|
||||||
|
view = ErrorView.as_view()
|
||||||
|
|
||||||
|
request = factory.get('/', content_type='application/json')
|
||||||
|
response = view(request)
|
||||||
|
expected = 'Error!'
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
self.assertEqual(response.data, expected)
|
||||||
|
|
||||||
|
def test_function_based_view_exception_handler(self):
|
||||||
|
view = error_view
|
||||||
|
|
||||||
|
request = factory.get('/', content_type='application/json')
|
||||||
|
response = view(request)
|
||||||
|
expected = 'Error!'
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
self.assertEqual(response.data, expected)
|
||||||
|
|
|
@ -361,7 +361,7 @@ class APIView(View):
|
||||||
else:
|
else:
|
||||||
exc.status_code = status.HTTP_403_FORBIDDEN
|
exc.status_code = status.HTTP_403_FORBIDDEN
|
||||||
|
|
||||||
response = exception_handler(exc)
|
response = self.settings.EXCEPTION_HANDLER(exc)
|
||||||
|
|
||||||
if response is None:
|
if response is None:
|
||||||
raise
|
raise
|
||||||
|
|
Loading…
Reference in New Issue
Block a user