diff options
Diffstat (limited to 'wsgitools')
-rw-r--r-- | wsgitools/middlewares.py | 20 |
1 files changed, 20 insertions, 0 deletions
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] |