Login

More informative error mailings

Author:
kcarnold
Posted:
March 7, 2008
Language:
Python
Version:
.96
Score:
6 (after 6 ratings)

This middleware makes the admin error emails a lot more informative: you get the same HTML response that you get with DEBUG=True.

It uses the base class defined in #638.

You will probably want to apply the patch for #6748 to help avoid slowdowns caused by unintentional database queries. As the ticket (and django-developers thread) notes, it isn't foolproof; you may still find this executing database queries.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from exception_middleware import import StandardExceptionMiddleware, _get_traceback
from django.conf import settings
from django.core.mail import EmailMultiAlternatives

class HTMLMailExceptionMiddleware(StandardExceptionMiddleware):
    def log_exception(self, request, exception, exc_info):
        debug_response = self.debug_500_response(request, exception, exc_info)

        subject, message = self.exception_email(request, exc_info)

        msg = EmailMultiAlternatives(settings.EMAIL_SUBJECT_PREFIX + subject,
                                     message,
                                     settings.SERVER_EMAIL,
                                     [a[1] for a in settings.ADMINS])

        msg.attach_alternative(debug_response.content, 'text/html')
        msg.send(fail_silently=True)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

kcarnold (on June 18, 2008):

Ignore the above comment; the snippet has been changed to reflect that.

#

henrikG (on August 12, 2008):

Howto use this middleware in your own project:

1. Download snippets 631 (this page) and 638.

2. Change the import line in the top of 631, so that 638 will be found, for example:

from myproject.middleware.638 import StandardExceptionMiddleware...

3. Add the middleware to myproject/settings.py:

MIDDLEWARE_CLASSES = ( myproject.middleware.631.HTMLMailExceptionMiddleware ... )

#

henrikG (on August 12, 2008):

Would it be possible to send the "debug_response.content" as an attachment in the mail, instead of as part of the message?

#

Please login first before commenting.