dcramer/raven — Issue #38: Pylons middleware
Author: mvantellingen
Created: 2012-01-20 13:01:42+00:00
State: open

Hi,

I've used the following Middleware for pylons, perhaps this could be included or noted in the docs.

```

import webob
from raven.middleware import Sentry, get_current_url
from raven.base import Client

class SentryMiddleware(Sentry):
"""Sentry middleware for pylons

Add the following settings in your config:

sentry.server =
sentry.site =
sentry.project = (optional)
sentry.public_key =
sentry.secret_key =

"""

def __init__(self, app, config):
client = Client(
servers=[config['sentry.server']],
public_key=config['sentry.public_key'],
secret_key=config['sentry.secret_key'],
site=config['sentry.site'],
project=config.get('sentry.project', 1),
include_paths=['grenslandauto']
)
super(SentryMiddleware, self).__init__(app, client)

def handle_exception(self, exc_info, environ):
request = webob.Request(environ)
event_id = self.client.create_from_exception(
exc_info=exc_info,
url=get_current_url(environ, strip_querystring=True),
data={
'culprit': '%s: %s' % (
exc_info[1].__class__.__name__, exc_info[1].message),
'sentry.interfaces.Http': {
'method': request.method,
'url': request.url,
'query_string': request.query_string,
'data': request.POST.mixed(),
'cookies': request.cookies.multi,
'headers': dict(request.headers),
'env': request.environ,
}
},
)
return event_id

```

This is for raven 1.0.4

Let me know if you want something like that in raven.contrib.pylons or something, then I'll create a pull request.

Thanks!