mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
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:
parent
dd712a1c26
commit
0d109c90a7
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user