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 <tom@tomchristie.com>
This commit is contained in:
Klaas van Schelven 2022-10-11 14:48:57 +02:00 committed by GitHub
parent 911b207fa1
commit 56946fac8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -79,9 +79,9 @@ def exception_handler(exc, context):
to be raised. to be raised.
""" """
if isinstance(exc, Http404): if isinstance(exc, Http404):
exc = exceptions.NotFound() exc = exceptions.NotFound(*(exc.args))
elif isinstance(exc, PermissionDenied): elif isinstance(exc, PermissionDenied):
exc = exceptions.PermissionDenied() exc = exceptions.PermissionDenied(*(exc.args))
if isinstance(exc, exceptions.APIException): if isinstance(exc, exceptions.APIException):
headers = {} headers = {}

View File

@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404
from django.test import TestCase from django.test import TestCase
from rest_framework import generics, renderers, serializers, status from rest_framework import generics, renderers, serializers, status
from rest_framework.exceptions import ErrorDetail
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory
from tests.models import ( from tests.models import (
@ -519,7 +520,12 @@ class TestFilterBackendAppliedToViews(TestCase):
request = factory.get('/1') request = factory.get('/1')
response = instance_view(request, pk=1).render() response = instance_view(request, pk=1).render()
assert response.status_code == status.HTTP_404_NOT_FOUND 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): def test_get_instance_view_will_return_single_object_when_filter_does_not_exclude_it(self):
""" """