Skip to content

Commit

Permalink
Django middlewares that differentiate between 404 and other exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rokob committed Jun 14, 2018
1 parent 2161b20 commit ec52dfd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rollbar/contrib/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,49 @@ def new_get_traceback_data(exception_reporter):
debug.ExceptionReporter.get_traceback_data = new_get_traceback_data


def _should_ignore_404(url):
url_patterns = getattr(settings, 'ROLLBAR', {}).get('ignorable_404_urls', ())
return any(p.search(url) for p in url_patterns)


class RollbarNotifierMiddlewareOnly404(MiddlewareMixin):
def get_extra_data(self, request, exc):
return

def get_payload_data(self, request, exc):
return

def process_response(self, request, response):
if response.status_code != 404:
return response

if _should_ignore_404(request.get_full_path()):
return response

try:
if hasattr(request, '_rollbar_notifier_original_http404_exc_info'):
exc_type, exc_value, exc_traceback = request._rollbar_notifier_original_http404_exc_info
raise exc_type, exc_value, exc_traceback
else:
raise Http404()
except Exception as exc:
rollbar.report_exc_info(
sys.exc_info(),
request,
extra_data=self.get_extra_data(request, exc),
payload_data=self.get_payload_data(request, exc),
)
return response


class RollbarNotifierMiddlewareExcluding404(RollbarNotifierMiddleware):
def process_exception(self, request, exc):
if isinstance(exc, Http404):
request._rollbar_notifier_original_http404_exc_info = sys.exc_info()
else:
super(RollbarNotifierMiddlewareExcluding404, self).process_exception(request, exc)


class RollbarNotifierMiddleware(MiddlewareMixin):
def __init__(self, get_response=None):
super(RollbarNotifierMiddleware, self).__init__(get_response)
Expand Down

0 comments on commit ec52dfd

Please sign in to comment.