diff --git a/rest_framework/schemas/views.py b/rest_framework/schemas/views.py index 845b68ea6d..f5e327a941 100644 --- a/rest_framework/schemas/views.py +++ b/rest_framework/schemas/views.py @@ -31,3 +31,11 @@ def get(self, request, *args, **kwargs): if schema is None: raise exceptions.PermissionDenied() return Response(schema) + + def handle_exception(self, exc): + # Schema renderers do not render exceptions, so re-perform content + # negotiation with default renderers. + self.renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES + neg = self.perform_content_negotiation(self.request, force=True) + self.request.accepted_renderer, self.request.accepted_media_type = neg + return super(SchemaView, self).handle_exception(exc) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 8e097f9f4b..d3bd430735 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -1304,3 +1304,13 @@ def test_foo(self): def test_FOO(self): assert not self._test('FOO') + + +@pytest.mark.skipif(not coreapi, reason='coreapi is not installed') +def test_schema_handles_exception(): + schema_view = get_schema_view(permission_classes=[DenyAllUsingPermissionDenied]) + request = factory.get('/') + response = schema_view(request) + response.render() + assert response.status_code == 403 + assert "You do not have permission to perform this action." in str(response.content)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: