Skip to content

Commit 7464deb

Browse files
committed
Fix pointer used by Http404 exception.
1 parent 5650bd1 commit 7464deb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

example/tests/test_model_viewsets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ def test_key_in_post(self):
212212
get_user_model().objects.get(pk=self.miles.pk).email,
213213
'miles@trumpet.org')
214214

215+
def test_404_error_pointer(self):
216+
"""
217+
Ensure 404 does not include pointer
218+
"""
219+
self.client.login(username='miles', password='pw')
220+
not_found_url = reverse('user-detail', kwargs={'pk': 12345})
221+
errors = {
222+
'errors': [
223+
{'detail': 'Not found.', 'status': '404'}
224+
]
225+
}
226+
227+
with override_settings(JSON_API_FORMAT_FIELD_NAMES='dasherize'):
228+
response = self.client.get(not_found_url)
229+
230+
assert 404 == response.status_code
231+
assert errors == response.json()
232+
215233

216234
@pytest.mark.django_db
217235
def test_patch_allow_field_type(author, author_type_factory, client):

rest_framework_json_api/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ManyToManyDescriptor,
1212
ReverseManyToOneDescriptor
1313
)
14+
from django.http import Http404
1415
from django.utils import encoding
1516
from django.utils.module_loading import import_string as import_class_from_dotted_path
1617
from django.utils.translation import ugettext_lazy as _
@@ -405,6 +406,12 @@ def format_drf_errors(response, context, exc):
405406
# see if they passed a dictionary to ValidationError manually
406407
if isinstance(error, dict):
407408
errors.append(error)
409+
elif isinstance(exc, Http404) and isinstance(error, str):
410+
# 404 errors don't have a pointer
411+
errors.append({
412+
'detail': error,
413+
'status': encoding.force_text(response.status_code),
414+
})
408415
elif isinstance(error, str):
409416
classes = inspect.getmembers(exceptions, inspect.isclass)
410417
# DRF sets the `field` to 'detail' for its own exceptions

0 commit comments

Comments
 (0)