Tweaking Django exceptions with custom middleware

When settings.DEBUG is set to False, exception tracebacks will be sent to settings.ADMINS. To make it simpler to track down how and why the exception was raised, it's beneficial to know which user caused the exception.

It's quite simple to do this using some custom middleware. In the Hub, we include the associated users email address in the exception with the following:

settings.py

MIDDLEWARE_CLASSES = (
    ...
    'hubutils.middleware.ExceptionMiddleware',
)

hubutils/middleware.py

class ExceptionMiddleware(object):
    def process_exception(self, request, exception):
        """include authenticated user email in request.META"""
        if request.user.is_authenticated():
            request.META['USER_EMAIL'] = request.user.email
You can get future posts delivered by email or good old-fashioned RSS.
TurnKey also has a presence on Google+, Twitter and Facebook.

Post new comment