Skip to content

Instantly share code, notes, and snippets.

@mattrobenolt
Forked from mallyvai/wsgi.py
Last active Oct 27, 2015
Embed
What would you like to do?
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from raven.utils.wsgi import (
get_current_url, get_headers, get_environ)
class Sentry(object):
def __init__(self, application):
self.application = application
@property
def client(self):
from raven.contrib.django.models import client
return client
def __call__(self, environ, start_response):
self.client.http_context(self.get_http_context(environ))
try:
iterable = self.application(environ, start_response)
except Exception:
self.handle_exception(environ)
raise
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
try:
for event in iterable:
yield event
except Exception:
self.handle_exception(environ)
raise
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
finally:
if iterable and hasattr(iterable, 'close') and callable(iterable.close):
try:
iterable.close()
except Exception:
self.handle_exception(environ)
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
self.client.context.clear()
def get_http_context(self, environ):
return {
'method': environ.get('REQUEST_METHOD'),
'url': get_current_url(environ, strip_querystring=True),
'query_string': environ.get('QUERY_STRING'),
'headers': dict(get_headers(environ)),
'env': dict(get_environ(environ)),
}
def process_response(self, request, response):
self.client.context.clear()
def handle_exception(self, environ=None):
return self.client.captureException()
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = Sentry(application)
application = DjangoWhiteNoise(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment