Skip to content

Commit 56946fa

Browse files
Preserve exception messages for wrapped Django exceptions (#8051)
* Preserve messages for wrapped Django exceptions * Fix the test * Update test_generics.py * Update test_generics.py Co-authored-by: Tom Christie <[email protected]>
1 parent 911b207 commit 56946fa

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

rest_framework/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def exception_handler(exc, context):
7979
to be raised.
8080
"""
8181
if isinstance(exc, Http404):
82-
exc = exceptions.NotFound()
82+
exc = exceptions.NotFound(*(exc.args))
8383
elif isinstance(exc, PermissionDenied):
84-
exc = exceptions.PermissionDenied()
84+
exc = exceptions.PermissionDenied(*(exc.args))
8585

8686
if isinstance(exc, exceptions.APIException):
8787
headers = {}

tests/test_generics.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.test import TestCase
66

77
from rest_framework import generics, renderers, serializers, status
8+
from rest_framework.exceptions import ErrorDetail
89
from rest_framework.response import Response
910
from rest_framework.test import APIRequestFactory
1011
from tests.models import (
@@ -519,7 +520,12 @@ def test_get_instance_view_filters_out_name_with_filter_backend(self):
519520
request = factory.get('/1')
520521
response = instance_view(request, pk=1).render()
521522
assert response.status_code == status.HTTP_404_NOT_FOUND
522-
assert response.data == {'detail': 'Not found.'}
523+
assert response.data == {
524+
'detail': ErrorDetail(
525+
string='No BasicModel matches the given query.',
526+
code='not_found'
527+
)
528+
}
523529

524530
def test_get_instance_view_will_return_single_object_when_filter_does_not_exclude_it(self):
525531
"""

0 commit comments

Comments
 (0)