error.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 02:49:06 +0200
changeset 135 19ff083c2870
parent 134 fbccc1648d79
child 136 c69a176b3620
permissions -rw-r--r--
pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Build error messages
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
     5
import traceback, sys, cgi, urllib
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
     7
def build_error (exc_info=None, env=None) :
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    """
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
        Dumps out a raw traceback of the given/current exception to stdout.
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    11
        If request_env, it should be a environment dict, like under WSGI, and will be used to display additional info
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    12
        about the request.
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    13
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
        Returns a (status, content-type, body) tuple, with all components being non-unicode strs.
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    """
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    17
    # default for exc_info is current exception
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    if not exc_info :
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        exc_info = sys.exc_info()
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    21
    # request URL?
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    22
    if env :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    23
        try :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    24
            from qmsk.web.http import request_url
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    25
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    26
            url = request_url(env)
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    27
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    28
        except :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    29
            # ignore
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    30
            url = None
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    31
    else :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    32
        url = None
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    33
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    34
    # working copy path?
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    35
    try :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    36
        from config import HG_WC_PATH
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    37
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    38
        wc_path = HG_WC_PATH
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    39
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    40
    except :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    41
        # a good guess
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    42
        wc_path = '.'
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    43
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    44
    # version?
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    45
    try :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    46
        from version import version_string
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    47
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    48
        version = version_string(wc_path)
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    49
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    50
    except :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    51
        version = None
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    52
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    53
    # the exception type
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    54
    exception_str = traceback.format_exception_only(*exc_info[:2])[-1][:255]
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    55
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    56
    # the exception traceback
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    57
    traceback_lines = traceback.format_exception(*exc_info)
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    58
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    59
    # XXX: make this configureable
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    60
    trac_url = "http://projects.qmsk.net/irclogs2/trac"
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    61
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    62
    # ticket list
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    63
    query_url = "%s/query" % trac_url
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    64
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    65
    # submit ticket
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    66
    submit_args = dict(type='defect')
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    67
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    68
    if url :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    69
        submit_args['url'] = url
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    70
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    71
    if version :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    72
        submit_args['revision'] = version
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    73
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    74
    if exception_str :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    75
        submit_args['summary'] = exception_str
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    76
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    77
    if traceback_lines :
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    78
        # this is big
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    79
        submit_args['description'] = """\
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    80
[Insert any additional information here]
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    81
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    82
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    83
= Traceback =
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    84
{{{
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    85
%s
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    86
}}}""" % ''.join(traceback_lines)
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    87
    
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    88
    # the trac newticket URL
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    89
    submit_url = "%s/newticket?%s" % (trac_url, '&amp;'.join('%s=%s' % (urllib.quote(k), urllib.quote(v)) for k, v in submit_args.iteritems()))
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    90
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    # return
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    return ('500 Internal Server Error', 'text/html; charset=UTF-8', ("""\
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
<html><head><title>500 Internal Server Error</title></head><body>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
<h1>Oops!</h1>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
<p>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    An error occured, which was not logged, and will not be reported to anybody. It might be your fault, or it might be
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
    97
    mine. 
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
</p>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
<p>
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   100
    You can try poking the administrator of this site to see if they respond, or submit a defect ticket at the <a href="%(query_url)s">Project Trac</a>, using the following link:
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
</p>
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   102
<pre>
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   103
    <a href="%(submit_url)s">%(submit_url_short)s</a>
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   104
</pre>
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
<h2>Details:</h2>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
<pre>%(traceback)s</pre>
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
</body></html>""" % dict(
135
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   108
        traceback           = cgi.escape(''.join(traceback_lines)),
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   109
        query_url           = query_url,
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   110
        submit_url          = submit_url,
19ff083c2870 pimp mah error messages, it has a full link to the Trac with pre-filled title, traceback, url, hg revision fields
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   111
        submit_url_short    = submit_url[:120] + '...'
134
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    )).encode('utf-8'))
fbccc1648d79 improved error handling for CGI/FastCGI
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113