mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Support handlers with and without context
This commit is contained in:
parent
0d109c90a7
commit
e8c0766568
|
@ -2,6 +2,7 @@
|
|||
Provides an APIView class that is the base of all views in REST framework.
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
import inspect
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import Http404
|
||||
|
@ -369,8 +370,13 @@ class APIView(View):
|
|||
else:
|
||||
exc.status_code = status.HTTP_403_FORBIDDEN
|
||||
|
||||
context = self.get_renderer_context()
|
||||
response = self.settings.EXCEPTION_HANDLER(exc, context)
|
||||
exception_handler = self.settings.EXCEPTION_HANDLER
|
||||
|
||||
if 'context' in inspect.getargspec(exception_handler).args:
|
||||
context = self.get_renderer_context()
|
||||
response = exception_handler(exc, context)
|
||||
else:
|
||||
response = exception_handler(exc)
|
||||
|
||||
if response is None:
|
||||
raise
|
||||
|
|
|
@ -6,6 +6,7 @@ from django.test import TestCase
|
|||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.settings import api_settings
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from rest_framework.views import APIView
|
||||
|
@ -121,12 +122,7 @@ class TestCustomExceptionHandler(TestCase):
|
|||
def setUp(self):
|
||||
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
|
||||
|
||||
def exception_handler(exc, context=None):
|
||||
self.assertTrue('args' in context)
|
||||
self.assertTrue('kwargs' in context)
|
||||
self.assertTrue('request' in context)
|
||||
self.assertTrue('view' in context)
|
||||
|
||||
def exception_handler(exc):
|
||||
return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
api_settings.EXCEPTION_HANDLER = exception_handler
|
||||
|
@ -151,3 +147,22 @@ class TestCustomExceptionHandler(TestCase):
|
|||
expected = 'Error!'
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertEqual(response.data, expected)
|
||||
|
||||
def test_context_exception_handler(self):
|
||||
def exception_handler(exc, context=None):
|
||||
self.assertEqual(context['args'], ())
|
||||
self.assertEqual(context['kwargs'], {})
|
||||
self.assertTrue(isinstance(context['request'], Request))
|
||||
self.assertTrue(isinstance(context['view'], ErrorView))
|
||||
|
||||
return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
api_settings.EXCEPTION_HANDLER = exception_handler
|
||||
|
||||
view = ErrorView.as_view()
|
||||
|
||||
request = factory.get('/', content_type='application/json')
|
||||
response = view(request)
|
||||
expected = 'Error!'
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertEqual(response.data, expected)
|
||||
|
|
Loading…
Reference in New Issue
Block a user