summaryrefslogtreecommitdiff
path: root/paste/wsgilib.py
diff options
context:
space:
mode:
authorianb <devnull@localhost>2005-07-11 21:15:16 +0000
committerianb <devnull@localhost>2005-07-11 21:15:16 +0000
commit1ac48b787dad6629a36e68532e2f758c20e30e25 (patch)
tree10fb9759017a43d9549b1c2cb16d63135853ef54 /paste/wsgilib.py
parentcea76a704df9063129d56478674cc3c2aa7a8464 (diff)
downloadpaste-1ac48b787dad6629a36e68532e2f758c20e30e25.tar.gz
Added new utility function to catch errors in subapplications, and use callbacks in those cases
Diffstat (limited to 'paste/wsgilib.py')
-rw-r--r--paste/wsgilib.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/paste/wsgilib.py b/paste/wsgilib.py
index 54f3cdf..e026b61 100644
--- a/paste/wsgilib.py
+++ b/paste/wsgilib.py
@@ -11,7 +11,8 @@ import cgi
__all__ = ['get_cookies', 'add_close', 'raw_interactive',
'interactive', 'construct_url', 'error_body_response',
'error_response', 'send_file', 'has_header', 'header_value',
- 'path_info_split', 'path_info_pop', 'capture_output']
+ 'path_info_split', 'path_info_pop', 'capture_output',
+ 'catch_errors']
def get_cookies(environ):
"""
@@ -73,6 +74,46 @@ class add_close:
self.app_iterable.close()
self.close_func()
+def catch_errors(application, environ, start_response, error_callback,
+ ok_callback=None):
+ """
+ Runs the application, and returns the application iterator (which should be
+ passed upstream). If an error occurs then error_callback will be called with
+ exc_info as its sole argument. If no errors occur and ok_callback is given,
+ then it will be called with no arguments.
+ """
+ error_occurred = False
+ try:
+ app_iter = application(environ, start_response)
+ except:
+ error_callback(sys.exc_info())
+ raise
+ return _wrap_app_iter(app_iter, error_callback, ok_callback)
+
+class _wrap_app_iter(object):
+
+ def __init__(self, app_iterable, error_callback, ok_callback):
+ self.app_iterable = app_iterable
+ self.app_iter = iter(app_iterable)
+ self.error_callback = error_callback
+ self.ok_callback = ok_callback
+ if hasattr(self.app_iterable, 'close'):
+ self.close = self.app_iterable.close
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ try:
+ return self.app_iter.next()
+ except StopIteration:
+ if self.ok_callback:
+ self.ok_callback()
+ raise
+ except:
+ self.error_callback(sys.exc_info())
+ raise
+
def raw_interactive(application, path_info='', **environ):
"""
Runs the application in a fake environment.