Skip to content

Commit

Permalink
fix exceptions in wrapped rest_framework exception handler
Browse files Browse the repository at this point in the history
- The request data is not guaranteed to be available in the exception
  handler, so this wraps the assignment to safely access the data.

- The import path could still result in a call to ``None``, this changes
  the handler to detect ``None`` and raise either a django
  ImproperlyConfigured or RuntimeError depending what is available.
  • Loading branch information
terencehonles committed May 31, 2020
1 parent 796d9c9 commit dcee846
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions rollbar/contrib/django_rest_framework/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
try:
from django.core.exceptions import ImproperlyConfigured
except ImportError:
RestFrameworkExceptionHandler = None
else:
try:
from rest_framework.views import exception_handler as RestFrameworkExceptionHandler
except (ImportError, ImproperlyConfigured):
RestFrameworkExceptionHandler = None
ImproperlyConfigured = RuntimeError

del ImproperlyConfigured
try:
from rest_framework.views import exception_handler as _exception_handler
except (ImportError, ImproperlyConfigured):
_exception_handler = None


def post_exception_handler(exc, context):
Expand All @@ -18,5 +16,14 @@ def post_exception_handler(exc, context):
# because we cannot read the request data/stream more than once.
# This will allow us to see the parsed POST params in the rollbar
# exception log.
context['request']._request.POST = context['request'].data
return RestFrameworkExceptionHandler(exc, context)

if _exception_handler is None:
raise ImproperlyConfigured(
'Could not import rest_framework.views.exception_handler')

try:
context['request']._request.POST = context['request'].data
except Exception:
pass

return _exception_handler(exc, context)

0 comments on commit dcee846

Please sign in to comment.