Skip to content

Commit 3f804c9

Browse files
committed
Fix pointer used by Http404 exception.
1 parent 435325e commit 3f804c9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-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 uses '/data' 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.', 'source': {'pointer': '/data'}, '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: 3 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, six
1516
from django.utils.module_loading import import_string as import_class_from_dotted_path
1617
from django.utils.translation import ugettext_lazy as _
@@ -413,7 +414,9 @@ def format_drf_errors(response, context, exc):
413414
errors.append(error)
414415
elif isinstance(error, six.string_types):
415416
classes = inspect.getmembers(exceptions, inspect.isclass)
417+
classes.append(['Http404', Http404])
416418
# DRF sets the `field` to 'detail' for its own exceptions
419+
# and some Django excpetions
417420
if isinstance(exc, tuple(x[1] for x in classes)):
418421
pointer = '/data'
419422
errors.append({

0 commit comments

Comments
 (0)