add better formatting of exceptions, use xtraceback package
* use xtraceback to format exceptions * log exceptions and environment *
This commit is contained in:
parent
227c0de8c5
commit
34d9bfad93
|
@ -66,6 +66,10 @@ You must install the following packages to use Mandaye
|
|||
|
||||
From sources: http://pypi.python.org/pypi/lxml
|
||||
|
||||
* xtraceback 0.3::
|
||||
|
||||
From sources: http://pypi.python.org/pypi/xtraceback
|
||||
|
||||
You can install all those dependencies quickly using pip::
|
||||
|
||||
pip install gevent poster SQLAlchemy Beaker Mako lxml
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
import logging
|
||||
import traceback
|
||||
import xtraceback
|
||||
import pprint
|
||||
import sys
|
||||
|
||||
from cgi import escape
|
||||
|
||||
|
@ -10,10 +11,12 @@ from mandaye.config import debug
|
|||
from template import serve_template
|
||||
|
||||
|
||||
def _get_html_traceback(exception, path):
|
||||
tb_str = traceback.format_exc()
|
||||
msg = "<h2>%s %s</h2><pre>%s</pre>" % \
|
||||
(path, exception, escape(tb_str))
|
||||
def _get_html_traceback(tb_str, path, env=None):
|
||||
msg = "<h2>%s</h2><h3>Exception</h3><pre>%s</pre>" % \
|
||||
(path, escape(tb_str))
|
||||
if env:
|
||||
env = pprint.pformat(env)
|
||||
msg += '<h3>Environment</h3><pre>%s</pre>' % escape(env)
|
||||
return msg
|
||||
|
||||
def _302(location, cookies=None):
|
||||
|
@ -37,17 +40,20 @@ def _404(path):
|
|||
return HTTPResponse(404, 'Not found', headers,
|
||||
serve_template("response.html", title=title, body=body))
|
||||
|
||||
def _500(path, msg, exception=None):
|
||||
def _500(path, msg, exception=None, env=None):
|
||||
""" Return a 500 error
|
||||
path: the path of this error
|
||||
exception: this exception instance if any """
|
||||
logging.critical("500 %s: %s" % (path, msg))
|
||||
if exception:
|
||||
tb_str = str(xtraceback.XTraceback(*sys.exc_info(), color=False))
|
||||
logging.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
|
||||
else:
|
||||
logging.error("500 %s: %s, env: %r" % (path, msg, env))
|
||||
title = 'Internal Server Error'
|
||||
headers = HTTPHeader({'Content-Type': ['text/html']})
|
||||
if debug:
|
||||
if exception:
|
||||
traceback.print_exc()
|
||||
msg += _get_html_traceback(exception, path)
|
||||
msg += _get_html_traceback(tb_str, path, env=env)
|
||||
else:
|
||||
msg=msg
|
||||
else:
|
||||
|
|
|
@ -82,6 +82,7 @@ class MandayeApp(object):
|
|||
env: standard WSGI env
|
||||
start_response: stanard WSGI / CGI function
|
||||
"""
|
||||
response = []
|
||||
try:
|
||||
self.env = env
|
||||
self.env['mandaye.scheme'] = env['wsgi.url_scheme']
|
||||
|
@ -99,18 +100,20 @@ class MandayeApp(object):
|
|||
elif mapper.has_key('static'):
|
||||
env['PATH_INFO'] = re.sub('^' + mapper.get('path'), '',
|
||||
env['PATH_INFO'])
|
||||
return Cling(mapper.get('static')).__call__(env, start_response)
|
||||
if self.dispatcher:
|
||||
return self.on_request(start_response)
|
||||
else:
|
||||
return self.on_response(start_response, _404(env['PATH_INFO']))
|
||||
except Exception, e:
|
||||
logging.exception('WSGI handler thrown an exception')
|
||||
sql_session.rollback()
|
||||
else:
|
||||
response = Cling(mapper.get('static')).__call__(env, start_response)
|
||||
if not response:
|
||||
if self.dispatcher:
|
||||
response = self.on_request(start_response)
|
||||
else:
|
||||
response = self.on_response(start_response, _404(env['PATH_INFO']))
|
||||
sql_session.commit()
|
||||
except Exception, e:
|
||||
sql_session.rollback()
|
||||
response = self.on_response(start_response, _500(env['PATH_INFO'], "Unhandled exception",
|
||||
exception=e, env=env))
|
||||
finally:
|
||||
sql_session.close()
|
||||
return response
|
||||
|
||||
|
||||
def _get_request(self):
|
||||
|
|
Reference in New Issue