Login

Detailed Error Reporting by Email

Author:
Archatas
Posted:
October 28, 2010
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.