Commit 0d109c90 by José Padilla

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.
parent dd712a1c
...@@ -46,7 +46,7 @@ def get_view_description(view_cls, html=False): ...@@ -46,7 +46,7 @@ def get_view_description(view_cls, html=False):
return description return description
def exception_handler(exc): def exception_handler(exc, context=None):
""" """
Returns the response that should be used for any given exception. Returns the response that should be used for any given exception.
...@@ -369,7 +369,8 @@ class APIView(View): ...@@ -369,7 +369,8 @@ class APIView(View):
else: else:
exc.status_code = status.HTTP_403_FORBIDDEN 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: if response is None:
raise raise
......
...@@ -121,7 +121,12 @@ class TestCustomExceptionHandler(TestCase): ...@@ -121,7 +121,12 @@ class TestCustomExceptionHandler(TestCase):
def setUp(self): def setUp(self):
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER 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) return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
api_settings.EXCEPTION_HANDLER = exception_handler api_settings.EXCEPTION_HANDLER = exception_handler
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment