Add context to exception handler #2236

Same context as renderers which include: the view,
args, kwargs, and request.

This provides enough contextual information to the
exception handlers to handle errors better.

In a use case like #1671, a custom handler
would allow Sentry to log the request properly.
This commit is contained in:
José Padilla 2014-12-13 18:18:00 -04:00
parent dd712a1c26
commit 0d109c90a7
2 changed files with 9 additions and 3 deletions

View File

@ -46,7 +46,7 @@ def get_view_description(view_cls, html=False):
return description
def exception_handler(exc):
def exception_handler(exc, context=None):
"""
Returns the response that should be used for any given exception.
@ -369,7 +369,8 @@ class APIView(View):
else:
exc.status_code = status.HTTP_403_FORBIDDEN
response = self.settings.EXCEPTION_HANDLER(exc)
context = self.get_renderer_context()
response = self.settings.EXCEPTION_HANDLER(exc, context)
if response is None:
raise

View File

@ -121,7 +121,12 @@ class TestCustomExceptionHandler(TestCase):
def setUp(self):
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
def exception_handler(exc):
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)
return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
api_settings.EXCEPTION_HANDLER = exception_handler