Login

ReportBug() (exception emails - ala django debug style)

Author:
sleepycal
Posted:
September 12, 2010
Language:
Python
Version:
1.2
Tags:
email debug mail exception report bug mail_admins reportbug
Score:
1 (after 1 ratings)

ReportBug() allows you to send exception details to you, via email, but with far more detail than the default. It uses the base function for the traceback used by the Debug mode on Django.

This is a first revision, so the emails have no decent styling, but it works, and shows scope on each stack.

It will automatically generate a random serial number per error, so you can track them in your favourite bug tracker. It also has support for you to pass it a request variable, so the mail would also contain request/response context. Again, i'm gonna look into doing this manually in the future.

Hope this helps!

Mwah.

Cal Leeming.

cal [at] simplicitymedialtd.co.uk.

Simplicity Media Ltd.

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
_FROM / _TO will define the email delivery settings.
Code can be easily adapted to use the values from settings.py.
The next revision will include this
"""
_FROM = 'My Errors <[email protected]>'
_TO = '[email protected]'

"""
ReportBug() allows you to send exception details to you, via email, but with
far more detail than the default. It uses the base function for the traceback
used by the Debug mode on Django.

This is a first revision, so the emails have no decent styling, but it works,
and shows scope on each stack. 

It will automatically generate a random serial number per error, so you can track them
in your favourite bug tracker. It also has support for you to pass it a request variable,
so the mail would also contain request/response context. Again, i'm gonna look into doing
this manually in the future.

Hope this helps!

Mwah.

Cal Leeming
cal [at] simplicitymedialtd.co.uk
Simplicity Media Ltd
"""

try:
    lol = 4
    raise Exception, "test"
except:
    ReportBug()
    raise

def ReportBug(request=None, serno=None):
    try:
        import sys
        import traceback
        import os

        # Mail the admins with the error
        exc_info = sys.exc_info()

        if exc_info:
            _file, _line, _func, _line = traceback.extract_tb(exc_info[2])[0]
            _file = os.path.basename(_file)

        else:
            _file, _line, _func, _line = (None, None, None, None)


        # Check if we have a serno
        if not serno:
            from hashlib import md5
            import random
            serno = md5()
            serno.update(str(random.random()))
            serno = serno.hexdigest()

        # When DEBUG is False, send an error message to the admins.
        subject = 'Exception in %s (line %s) (ID: %s)' % (
            _file,
            _line,
            serno
        )

        message = 'Traceback:\n%s\n\n' % ('\n'.join(traceback.format_exception(*exc_info)),)

        import sys
        from django.views.debug import ExceptionReporter
        from django.http import HttpRequest
        from django.conf import settings
        from django.core.mail import EmailMultiAlternatives
        if not request:
            h = HttpRequest()
            h.META['SERVER_NAME'] = 'FAKE'
            h.META['SERVER_PORT'] = '80'
        else:
            h = request
            
        exp = sys.exc_info()
        t = ExceptionReporter(h, *exp)

        subject, from_email, to = '[Django] %s'%(str(exp[1])), _FROM, _TO
        text_content = message
        html_content = t.get_traceback_html()
        msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
        msg.attach_alternative(html_content, "text/html")
        msg.send()

    except Exception, e:
        mail_admins("SERIOUS ERROR", "Not sure what happened.. %s"%str(e), fail_silently=True)

More like this

  1. Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 6 months, 1 week ago
  2. Crispy Form by sourabhsinha396 7 months ago
  3. ReadOnlySelect by mkoistinen 7 months, 2 weeks ago
  4. Verify events sent to your webhook endpoints by santos22 8 months, 1 week ago
  5. Django Language Middleware by agusmakmun 8 months, 3 weeks ago

Comments

buriy (on September 12, 2010):

why not use uuid for really good serial number?

#

Please login first before commenting.