mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Merge 87b08846e2
into fd72a814f8
This commit is contained in:
commit
4704da5030
|
@ -84,6 +84,7 @@ DEFAULTS = {
|
||||||
# Exception handling
|
# Exception handling
|
||||||
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
|
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
|
||||||
'NON_FIELD_ERRORS_KEY': 'non_field_errors',
|
'NON_FIELD_ERRORS_KEY': 'non_field_errors',
|
||||||
|
'EXCEPTION_DETAIL_KEY': 'detail',
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
'TEST_REQUEST_RENDERER_CLASSES': (
|
'TEST_REQUEST_RENDERER_CLASSES': (
|
||||||
|
|
|
@ -64,6 +64,7 @@ def exception_handler(exc, context):
|
||||||
Any unhandled exceptions may return `None`, which will cause a 500 error
|
Any unhandled exceptions may return `None`, which will cause a 500 error
|
||||||
to be raised.
|
to be raised.
|
||||||
"""
|
"""
|
||||||
|
exception_detail_key = api_settings.EXCEPTION_DETAIL_KEY
|
||||||
if isinstance(exc, exceptions.APIException):
|
if isinstance(exc, exceptions.APIException):
|
||||||
headers = {}
|
headers = {}
|
||||||
if getattr(exc, 'auth_header', None):
|
if getattr(exc, 'auth_header', None):
|
||||||
|
@ -74,21 +75,21 @@ def exception_handler(exc, context):
|
||||||
if isinstance(exc.detail, (list, dict)):
|
if isinstance(exc.detail, (list, dict)):
|
||||||
data = exc.detail
|
data = exc.detail
|
||||||
else:
|
else:
|
||||||
data = {'detail': exc.detail}
|
data = {exception_detail_key: exc.detail}
|
||||||
|
|
||||||
set_rollback()
|
set_rollback()
|
||||||
return Response(data, status=exc.status_code, headers=headers)
|
return Response(data, status=exc.status_code, headers=headers)
|
||||||
|
|
||||||
elif isinstance(exc, Http404):
|
elif isinstance(exc, Http404):
|
||||||
msg = _('Not found.')
|
msg = _('Not found.')
|
||||||
data = {'detail': six.text_type(msg)}
|
data = {exception_detail_key: six.text_type(msg)}
|
||||||
|
|
||||||
set_rollback()
|
set_rollback()
|
||||||
return Response(data, status=status.HTTP_404_NOT_FOUND)
|
return Response(data, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
elif isinstance(exc, PermissionDenied):
|
elif isinstance(exc, PermissionDenied):
|
||||||
msg = _('Permission denied.')
|
msg = _('Permission denied.')
|
||||||
data = {'detail': six.text_type(msg)}
|
data = {exception_detail_key: six.text_type(msg)}
|
||||||
|
|
||||||
set_rollback()
|
set_rollback()
|
||||||
return Response(data, status=status.HTTP_403_FORBIDDEN)
|
return Response(data, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user