diff --git a/README.rst b/README.rst index a10f428..a11bb89 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/mandaye/response.py b/mandaye/response.py index 1f9af1d..227dc52 100644 --- a/mandaye/response.py +++ b/mandaye/response.py @@ -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 = "

%s %s

%s
" % \ - (path, exception, escape(tb_str)) +def _get_html_traceback(tb_str, path, env=None): + msg = "

%s

Exception

%s
" % \ + (path, escape(tb_str)) + if env: + env = pprint.pformat(env) + msg += '

Environment

%s
' % 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: diff --git a/mandaye/server.py b/mandaye/server.py index 17a632d..b89eed8 100644 --- a/mandaye/server.py +++ b/mandaye/server.py @@ -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):