aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/views.py
diff options
context:
space:
mode:
authorEleni Lixourioti2014-11-15 14:27:41 +0000
committerEleni Lixourioti2014-11-15 14:27:41 +0000
commit1aa77830955dcdf829f65a9001b6b8900dfc8755 (patch)
tree1f6d0bea3c0fe720a298b2da177bb91e8a74a19c /rest_framework/views.py
parentafaa52a378705b7f0475d5ece04a2cf49af4b7c2 (diff)
parent88008c0a687219e3104d548196915b1068536d74 (diff)
downloaddjango-rest-framework-1aa77830955dcdf829f65a9001b6b8900dfc8755.tar.bz2
Merge branch 'version-3.1' of github.com:tomchristie/django-rest-framework into oauth_as_package
Conflicts: .travis.yml
Diffstat (limited to 'rest_framework/views.py')
-rw-r--r--rest_framework/views.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/rest_framework/views.py b/rest_framework/views.py
index 38346ab7..9f08a4ad 100644
--- a/rest_framework/views.py
+++ b/rest_framework/views.py
@@ -3,7 +3,7 @@ Provides an APIView class that is the base of all views in REST framework.
"""
from __future__ import unicode_literals
-from django.core.exceptions import PermissionDenied
+from django.core.exceptions import PermissionDenied, ValidationError, NON_FIELD_ERRORS
from django.http import Http404
from django.utils.datastructures import SortedDict
from django.views.decorators.csrf import csrf_exempt
@@ -51,7 +51,8 @@ def exception_handler(exc):
Returns the response that should be used for any given exception.
By default we handle the REST framework `APIException`, and also
- Django's builtin `Http404` and `PermissionDenied` exceptions.
+ Django's built-in `ValidationError`, `Http404` and `PermissionDenied`
+ exceptions.
Any unhandled exceptions may return `None`, which will cause a 500 error
to be raised.
@@ -61,13 +62,22 @@ def exception_handler(exc):
if getattr(exc, 'auth_header', None):
headers['WWW-Authenticate'] = exc.auth_header
if getattr(exc, 'wait', None):
- headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
headers['Retry-After'] = '%d' % exc.wait
return Response({'detail': exc.detail},
status=exc.status_code,
headers=headers)
+ elif isinstance(exc, ValidationError):
+ # ValidationErrors may include the non-field key named '__all__'.
+ # When returning a response we map this to a key name that can be
+ # modified in settings.
+ if NON_FIELD_ERRORS in exc.message_dict:
+ errors = exc.message_dict.pop(NON_FIELD_ERRORS)
+ exc.message_dict[api_settings.NON_FIELD_ERRORS_KEY] = errors
+ return Response(exc.message_dict,
+ status=status.HTTP_400_BAD_REQUEST)
+
elif isinstance(exc, Http404):
return Response({'detail': 'Not found'},
status=status.HTTP_404_NOT_FOUND)