terom@134: """ terom@134: Build error messages terom@134: """ terom@134: terom@135: import traceback, sys, cgi, urllib terom@134: terom@135: def build_error (exc_info=None, env=None) : terom@134: """ terom@134: Dumps out a raw traceback of the given/current exception to stdout. terom@134: terom@135: If request_env, it should be a environment dict, like under WSGI, and will be used to display additional info terom@135: about the request. terom@135: terom@134: Returns a (status, content-type, body) tuple, with all components being non-unicode strs. terom@134: """ terom@134: terom@135: # default for exc_info is current exception terom@134: if not exc_info : terom@134: exc_info = sys.exc_info() terom@134: terom@135: # request URL? terom@135: if env : terom@135: try : terom@135: from qmsk.web.http import request_url terom@135: terom@135: url = request_url(env) terom@135: terom@135: except : terom@135: # ignore terom@135: url = None terom@135: else : terom@135: url = None terom@135: terom@135: # working copy path? terom@135: try : terom@135: from config import HG_WC_PATH terom@135: terom@135: wc_path = HG_WC_PATH terom@135: terom@135: except : terom@135: # a good guess terom@135: wc_path = '.' terom@135: terom@135: # version? terom@135: try : terom@135: from version import version_string terom@135: terom@135: version = version_string(wc_path) terom@135: terom@135: except : terom@135: version = None terom@135: terom@135: # the exception type terom@135: exception_str = traceback.format_exception_only(*exc_info[:2])[-1][:255] terom@135: terom@135: # the exception traceback terom@135: traceback_lines = traceback.format_exception(*exc_info) terom@135: terom@135: # XXX: make this configureable terom@135: trac_url = "http://projects.qmsk.net/irclogs2/trac" terom@135: terom@135: # ticket list terom@135: query_url = "%s/query" % trac_url terom@135: terom@135: # submit ticket terom@135: submit_args = dict(type='defect') terom@135: terom@135: if url : terom@135: submit_args['url'] = url terom@135: terom@135: if version : terom@135: submit_args['revision'] = version terom@135: terom@135: if exception_str : terom@135: submit_args['summary'] = exception_str terom@135: terom@135: if traceback_lines : terom@135: # this is big terom@135: submit_args['description'] = """\ terom@135: [Insert any additional information here] terom@135: terom@135: terom@135: = Traceback = terom@135: {{{ terom@135: %s terom@135: }}}""" % ''.join(traceback_lines) terom@135: terom@135: # the trac newticket URL terom@135: submit_url = "%s/newticket?%s" % (trac_url, '&'.join('%s=%s' % (urllib.quote(k), urllib.quote(v)) for k, v in submit_args.iteritems())) terom@135: terom@134: # return terom@134: return ('500 Internal Server Error', 'text/html; charset=UTF-8', ("""\ terom@134: 500 Internal Server Error terom@134:

Oops!

terom@134:

terom@134: An error occured, which was not logged, and will not be reported to anybody. It might be your fault, or it might be terom@135: mine. terom@134:

terom@134:

terom@135: You can try poking the administrator of this site to see if they respond, or submit a defect ticket at the Project Trac, using the following link: terom@134:

terom@135:
terom@135:     %(submit_url_short)s
terom@135: 
terom@134:

Details:

terom@134:
%(traceback)s
terom@134: """ % dict( terom@135: traceback = cgi.escape(''.join(traceback_lines)), terom@135: query_url = query_url, terom@135: submit_url = submit_url, terom@135: submit_url_short = submit_url[:120] + '...' terom@134: )).encode('utf-8')) terom@134: