From 293a9e81bad7fe6f53980e229eed677b4101b846 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sat, 14 Apr 2007 23:35:49 +0200 Subject: added TracebackMiddleware --- wsgitools/middlewares.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'wsgitools/middlewares.py') diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 9eab2a1..ea61aca 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -1,6 +1,8 @@ __all__ = [] import time +import sys +import cgitb from filters import CloseableList, CloseableIterator try: import cStringIO as StringIO @@ -265,3 +267,21 @@ class BasicAuthMiddleware: headers.append(('Content-length', len(html))) start_response(status, headers) return [html] + +__all__.append("TracebackMiddleware") +class TracebackMiddleware: + """In case the application throws an exception this middleware will show an + html-formatted traceback using cgitb.""" + def __init__(self, app): + """app is the wsgi application to proxy.""" + self.app = app + def __call__(self, environ, start_response): + """wsgi interface""" + try: + return self.app(environ, start_response) + except: + exc_info = sys.exc_info() + data = cgitb.html(exc_info) + start_response("200 OK", [("Content-type", "text/html"), + ("Content-length", str(len(data)))]) + return [data] -- cgit v1.2.3