From 36593a8d40cd1417d2927a78cdbe621090b6e8a5 Mon Sep 17 00:00:00 2001 From: ianb Date: Fri, 22 Apr 2005 03:18:20 +0000 Subject: Renamed package itself --- paste/cgitb_catcher.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 paste/cgitb_catcher.py (limited to 'paste/cgitb_catcher.py') diff --git a/paste/cgitb_catcher.py b/paste/cgitb_catcher.py new file mode 100644 index 0000000..b8f4915 --- /dev/null +++ b/paste/cgitb_catcher.py @@ -0,0 +1,95 @@ +""" +WSGI middleware + +Captures any exceptions and prints a pretty report. See the cgitb +documentation for more: + http://python.org/doc/current/lib/module-cgitb.html +""" + +import cgitb +from cStringIO import StringIO +import sys +import traceback + +class DummyFile(object): + pass + +def middleware(application, **kw): + + def start_application(environ, start_response): + started = [] + + def detect_start_response(status, headers): + started.append(start_response(status, headers)) + return started[0] + + try: + app_iter = application(environ, start_response) + return catching_iter(app_iter) + except: + if not started: + write = start_response('500 Internal Server Error', + [('content-type', 'text/html')]) + else: + write = started[0] + dummy_file = DummyFile() + dummy_file.write = write + dummy_file = StringIO() + hook = cgitb.Hook(**kw) + hook.file = dummy_file + hook(*sys.exc_info()) + return [dummy_file.getvalue()] + + def catching_iter(iter): + if not iter: + raise StopIteration + try: + for v in iter: + yield iter + except: + exc = sys.exc_info() + dummy_file = StringIO() + hook = cgitb.Hook(**kw) + hook.file = dummy_file + hook(*exc) + yield dummy_file.getvalue() + + return start_application + +def simple_middleware(application, **kw): + + def start_application(environ, start_response): + started = [] + + def detect_start_response(status, headers): + started.append(start_response(status, headers)) + return started[0] + + try: + app_iter = application(environ, start_response) + return catching_iter(app_iter) + except: + if not started: + write = start_response('500 Internal Server Error', + [('content-type', 'text/html')]) + else: + write = started[0] + + out = String() + traceback.print_exc(file=out) + return ['
%s
' + % out.getvalue()] + + def catching_iter(iter): + if not iter: + raise StopIteration + try: + for v in iter: + yield iter + except: + exc = sys.exc_info() + dummy_file = StringIO() + traceback.print_exc(file=dummy_file) + yield dummy_file.getvalue() + + return start_application -- cgit v1.2.1