error.py
changeset 137 a25d1bf758e6
parent 136 c69a176b3620
child 139 9c7769850195
equal deleted inserted replaced
136:c69a176b3620 137:a25d1bf758e6
    12     if len(msg) > limit :
    12     if len(msg) > limit :
    13         return msg[:limit-3] + '...'
    13         return msg[:limit-3] + '...'
    14 
    14 
    15     else :
    15     else :
    16         return msg
    16         return msg
       
    17 
       
    18 def build_link (title, url) :
       
    19     return '<a href="%s">%s</a>' % (cgi.escape(url, True), cgi.escape(title))
    17 
    20 
    18 def build_error (exc_info=None, env=None) :
    21 def build_error (exc_info=None, env=None) :
    19     """
    22     """
    20         Dumps out a raw traceback of the given/current exception to stdout.
    23         Dumps out a raw traceback of the given/current exception to stdout.
    21 
    24 
    42     else :
    45     else :
    43         url = None
    46         url = None
    44 
    47 
    45     # working copy path?
    48     # working copy path?
    46     try :
    49     try :
    47         from config import HG_WC_PATH
    50         from config import HG_WC_PATH, HGWEB_URL
    48 
    51 
    49         wc_path = HG_WC_PATH
    52         wc_path = HG_WC_PATH
       
    53         hgweb_url = HGWEB_URL
    50 
    54 
    51     except :
    55     except :
    52         # a good guess
    56         # a good guess
    53         wc_path = '.'
    57         wc_path = '.'
       
    58         hgweb_url = None
    54     
    59     
    55     # version?
    60     # version?
    56     try :
    61     try :
    57         from version import version_string
    62         from version import version_string, version_link_hg
    58 
    63 
    59         version = version_string(wc_path)
    64         version = version_string(wc_path)
       
    65 
       
    66         if hgweb_url :
       
    67             version_href = version_link_hg(wc_path, hgweb_url)
       
    68 
       
    69         else :
       
    70             version_href = None
    60     
    71     
    61     except :
    72     except :
    62         version = None
    73         version = None
       
    74         version_href = None
    63     
    75     
    64     # the exception type
    76     # the exception type
    65     exception_str = traceback.format_exception_only(*exc_info[:2])[-1]
    77     exception_str = traceback.format_exception_only(*exc_info[:2])[-1]
    66 
    78 
    67     # the exception traceback
    79     # the exception traceback
    69 
    81 
    70     # XXX: make this configureable
    82     # XXX: make this configureable
    71     trac_url = "http://projects.qmsk.net/irclogs2/trac"
    83     trac_url = "http://projects.qmsk.net/irclogs2/trac"
    72     
    84     
    73     # ticket list
    85     # ticket list
    74     query_url = "%s/query" % trac_url
    86     trac_query = build_link("All tickets", "%s/query" % trac_url)
    75 
    87 
    76     # submit ticket
    88     # submit ticket
    77     submit_args = dict(type='defect')
    89     submit_args = dict(type='defect')
    78 
    90     
       
    91     # handle optional components
    79     if url :
    92     if url :
    80         submit_args['url'] = url
    93         submit_args['url'] = url
    81 
    94         trac_query_url = build_link("Same URL", "%s/query?url=%s" % (trac_url, urllib.quote(url)))
       
    95     else :
       
    96         trac_query_url = ""
       
    97     
    82     if version :
    98     if version :
    83         submit_args['revision'] = version
    99         submit_args['revision'] = version
       
   100         trac_query_version = build_link("Same version", "%s/query?revision=%s" % (trac_url, urllib.quote(version)))
       
   101 
       
   102     else :
       
   103         trac_query_version = ""
    84     
   104     
    85     if exception_str :
   105     if exception_str :
    86         submit_args['summary'] = truncate(exception_str, 140)
   106         submit_args['summary'] = truncate(exception_str, 140)
       
   107         trac_query_err = build_link("Same error", "%s/query?summary=%s" % (trac_url, urllib.quote(exception_str.rstrip())))
       
   108 
       
   109     else :
       
   110         trac_query_err = ""
    87 
   111 
    88     if traceback_lines :
   112     if traceback_lines :
    89         # this is big
   113         # this is big
    90         submit_args['description'] = """\
   114         submit_args['description'] = """\
    91 [Insert any additional information here]
   115 [Insert any additional information here]
   102     # return
   126     # return
   103     return ('500 Internal Server Error', 'text/html; charset=UTF-8', ("""\
   127     return ('500 Internal Server Error', 'text/html; charset=UTF-8', ("""\
   104 <html><head><title>500 Internal Server Error</title></head><body>
   128 <html><head><title>500 Internal Server Error</title></head><body>
   105 <h1>Oops!</h1>
   129 <h1>Oops!</h1>
   106 <p>
   130 <p>
   107     An error occured, which was not logged, and will not be reported to anybody. It might be your fault, or it might be
   131     An error occured, which was not logged, and was not reported to anybody. It might be your fault, or it might be mine.
   108     mine. 
       
   109 </p>
   132 </p>
       
   133 
   110 <p>
   134 <p>
   111     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:
   135     You can try:
       
   136     <ol style="list-style-type: lower-alpha">
       
   137         <li><strong>Poking</strong> the administrator of this site to see if they respond</li>
       
   138         <li><strong>Looking</strong> for similar issue tickets with:
       
   139           <ul>
       
   140             <li>%(trac_query)s</li>
       
   141             <li>%(trac_query_url)s</li>
       
   142             <li>%(trac_query_version)s</li>
       
   143             <li>%(trac_query_err)s</li>
       
   144           </ul>
       
   145         </li>
       
   146         <li><strong>Submitting</strong> a new ticket using the following link (quick &amp; easy):</li>
       
   147     </ol>
   112 </p>
   148 </p>
   113 <pre>
   149 <pre>
   114     <a href="%(submit_url)s">%(submit_url_short)s</a>
   150     <a href="%(submit_url)s">%(submit_url_short)s</a>
   115 </pre>
   151 </pre>
       
   152 
   116 <h2>Details:</h2>
   153 <h2>Details:</h2>
       
   154 <p>The page you tried to request was:</p>
       
   155 <pre>
       
   156     %(url)s
       
   157 </pre>
       
   158 
       
   159 <p>The software version is:</p>
       
   160 <pre>
       
   161     %(version_link)s
       
   162 </pre>
       
   163 
       
   164 <p>The error was:</p>
       
   165 <pre>
       
   166     %(exception)s
       
   167 </pre>
       
   168 
       
   169 <p>The traceback was:</p>
   117 <pre>%(traceback)s</pre>
   170 <pre>%(traceback)s</pre>
   118 </body></html>""" % dict(
   171 </body></html>""" % dict(
   119         traceback           = cgi.escape(''.join(traceback_lines)),
   172         url                 = url if url else 'Unknown',
   120         query_url           = query_url,
   173         version_link        = version_href if version_href else 'Unknown',
       
   174         exception           = truncate(exception_str, 512),
       
   175         traceback           = cgi.escape(''.join('   ' + line for line in traceback_lines)),
       
   176         trac_query          = trac_query,
       
   177         trac_query_url      = trac_query_url,
       
   178         trac_query_version  = trac_query_version,
       
   179         trac_query_err      = trac_query_err,
   121         submit_url          = submit_url,
   180         submit_url          = submit_url,
   122         submit_url_short    = truncate(submit_url, 120)
   181         submit_url_short    = truncate(submit_url, 120)
   123     )).encode('utf-8'))
   182     )).encode('utf-8'))
   124 
   183