Skip to content

Commit df77f7b

Browse files
Si Fengtomchristie
authored andcommitted
Make 404 & 403 responses consistent with exceptions.APIException output (#5763)
1 parent ece4171 commit df77f7b

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

rest_framework/views.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
from django.db import connection, models, transaction
99
from django.http import Http404
1010
from django.http.response import HttpResponseBase
11-
from django.utils import six
1211
from django.utils.cache import cc_delim_re, patch_vary_headers
1312
from django.utils.encoding import smart_text
14-
from django.utils.translation import ugettext_lazy as _
1513
from django.views.decorators.csrf import csrf_exempt
1614
from django.views.generic import View
1715

@@ -70,6 +68,11 @@ def exception_handler(exc, context):
7068
Any unhandled exceptions may return `None`, which will cause a 500 error
7169
to be raised.
7270
"""
71+
if isinstance(exc, Http404):
72+
exc = exceptions.NotFound()
73+
elif isinstance(exc, PermissionDenied):
74+
exc = exceptions.PermissionDenied()
75+
7376
if isinstance(exc, exceptions.APIException):
7477
headers = {}
7578
if getattr(exc, 'auth_header', None):
@@ -85,20 +88,6 @@ def exception_handler(exc, context):
8588
set_rollback()
8689
return Response(data, status=exc.status_code, headers=headers)
8790

88-
elif isinstance(exc, Http404):
89-
msg = _('Not found.')
90-
data = {'detail': six.text_type(msg)}
91-
92-
set_rollback()
93-
return Response(data, status=status.HTTP_404_NOT_FOUND)
94-
95-
elif isinstance(exc, PermissionDenied):
96-
msg = _('Permission denied.')
97-
data = {'detail': six.text_type(msg)}
98-
99-
set_rollback()
100-
return Response(data, status=status.HTTP_403_FORBIDDEN)
101-
10291
return None
10392

10493

0 commit comments

Comments
 (0)