diff options
author | ianb <devnull@localhost> | 2005-08-22 21:49:38 +0000 |
---|---|---|
committer | ianb <devnull@localhost> | 2005-08-22 21:49:38 +0000 |
commit | 727dfaaa38a0b3b88b932726f782769de2af8f3b (patch) | |
tree | 4adc76d60248d17419361bdf8b350332e41adc57 /paste/cgitb_catcher.py | |
parent | 2b234d342643c388d7c97324493e6344ed1bc367 (diff) | |
download | paste-727dfaaa38a0b3b88b932726f782769de2af8f3b.tar.gz |
Tests and bugfixes for cgitb_catcher
Diffstat (limited to 'paste/cgitb_catcher.py')
-rw-r--r-- | paste/cgitb_catcher.py | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/paste/cgitb_catcher.py b/paste/cgitb_catcher.py index a245299..43df21c 100644 --- a/paste/cgitb_catcher.py +++ b/paste/cgitb_catcher.py @@ -22,7 +22,7 @@ class CgitbMiddleware(object): logdir=None, context=5, format="html"): - self.app = application + self.app = app if display is NoDefault: display = global_conf.get('debug') self.display = converters.asbool(display) @@ -33,34 +33,45 @@ class CgitbMiddleware(object): def __call__(self, environ, start_response): try: app_iter = self.app(environ, start_response) - return catching_iter(app_iter) + return self.catching_iter(app_iter, environ) except: exc_info = sys.exc_info() start_response('500 Internal Server Error', [('content-type', 'text/html')], exc_info) - dummy_file = StringIO() - hook = cgitb.Hook(file=dummy_file, - display=self.display, - logdir=self.logdir, - context=self.context, - format=self.format) - hook(*exc_info) - return [dummy_file.getvalue()] + response = self.exception_handler(exc_info, environ) + return [response] - def catching_iter(self, app_iter): + def catching_iter(self, app_iter, environ): if not app_iter: raise StopIteration + error_on_close = False try: for v in app_iter: yield v + if hasattr(app_iter, 'close'): + error_on_close = True + app_iter.close() except: - exc_info = sys.exc_info() - dummy_file = StringIO() - hook = cgitb.Hook(file=dummy_file, - display=self.display, - logdir=self.logdir, - context=self.context, - format=self.format) - hook(*exc_info) - yield dummy_file.getvalue() + response = self.exception_handler(sys.exc_info(), environ) + if not error_on_close and hasattr(app_iter, 'close'): + try: + app_iter.close() + except: + close_response = self.exception_handler( + sys.exc_info(), environ) + response += ( + '<hr noshade>Error in .close():<br>%s' + % close_response) + yield response + + def exception_handler(self, exc_info, environ): + dummy_file = StringIO() + hook = cgitb.Hook(file=dummy_file, + display=self.display, + logdir=self.logdir, + context=self.context, + format=self.format) + hook(*exc_info) + return dummy_file.getvalue() + |