Login

Detailed Error Reporting by Email

Author:
Archatas
Posted:
October 28, 2010
Language:
Python
Version:
1.2
Tags:
email error reporting
Score:
5 (after 5 ratings)

The default traceback sent by email when an error occurs, usually gives too little information comparing it to the error page in the DEBUG mode. This snippet guerilla-patches error handling and sends by email the same information as you would see in DEBUG mode.

To set it up, add the snippet to any models.py of an installed app.

(I wonder why this hasn't been implemented in the core)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: UTF-8 -*-

from django.core.handlers.base import BaseHandler

def _handle_uncaught_exception(self, request, resolver, exc_info):
    """
    Processing for any otherwise uncaught exceptions (those that will
    generate HTTP 500 responses). Can be overridden by subclasses who want
    customised 500 handling.

    Be *very* careful when overriding this because the error could be
    caused by anything, so assuming something like the database is always
    available would be an error.
    """
    from django.conf import settings
    from django.core.mail.message import EmailMessage
    from django.views import debug

    if settings.DEBUG_PROPAGATE_EXCEPTIONS:
        raise

    technical_500_response = debug.technical_500_response(request, *exc_info)

    if settings.DEBUG:
        return technical_500_response

    # When DEBUG is False, send an error message to the admins.
    subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
    
    msg = EmailMessage(
        settings.EMAIL_SUBJECT_PREFIX + subject,
        technical_500_response.content,
        settings.SERVER_EMAIL,
        [a[1] for a in settings.ADMINS],
        )
    msg.content_subtype = "html"  # Main content is now text/html
    msg.send(fail_silently=True)

    # If Http500 handler is not installed, re-raise last exception
    if resolver.urlconf_module is None:
        raise exc_info[1], None, exc_info[2]
    # Return an HttpResponse that displays a friendly error message.
    callback, param_dict = resolver.resolve500()
    return callback(request, **param_dict)

BaseHandler.handle_uncaught_exception = _handle_uncaught_exception

More like this

  1. More informative error mailings by kcarnold 7 years ago
  2. ReportBug() (exception emails - ala django debug style) by sleepycal 4 years, 6 months ago
  3. ajax error sink by amitu 6 years, 5 months ago
  4. ReportBug() with tons of debug in mail by sleepycal 4 years, 5 months ago
  5. Extensible exception handling middleware by kcarnold 7 years ago

Comments

Please login first before commenting.