Skip to content

Commit

Permalink
Preserve exception messages for wrapped Django exceptions (#8051)
Browse files Browse the repository at this point in the history
* Preserve messages for wrapped Django exceptions

* Fix the test

* Update test_generics.py

* Update test_generics.py

Co-authored-by: Tom Christie <tom@tomchristie.com>
  • Loading branch information
vanschelven and tomchristie committed Oct 11, 2022
1 parent 911b207 commit 56946fa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def exception_handler(exc, context):
to be raised.
"""
if isinstance(exc, Http404):
exc = exceptions.NotFound()
exc = exceptions.NotFound(*(exc.args))
elif isinstance(exc, PermissionDenied):
exc = exceptions.PermissionDenied()
exc = exceptions.PermissionDenied(*(exc.args))

if isinstance(exc, exceptions.APIException):
headers = {}
Expand Down
8 changes: 7 additions & 1 deletion tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test import TestCase

from rest_framework import generics, renderers, serializers, status
from rest_framework.exceptions import ErrorDetail
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from tests.models import (
Expand Down Expand Up @@ -519,7 +520,12 @@ def test_get_instance_view_filters_out_name_with_filter_backend(self):
request = factory.get('/1')
response = instance_view(request, pk=1).render()
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.data == {'detail': 'Not found.'}
assert response.data == {
'detail': ErrorDetail(
string='No BasicModel matches the given query.',
code='not_found'
)
}

def test_get_instance_view_will_return_single_object_when_filter_does_not_exclude_it(self):
"""
Expand Down

0 comments on commit 56946fa

Please sign in to comment.