77
88from characteristic import attributes
99
10+ import six
11+
1012from ._continuation import trampoline
1113
1214
@@ -26,7 +28,7 @@ def __init__(self, intent, callbacks=None):
2628 """
2729 self .intent = intent
2830 if callbacks is None :
29- callbacks = ()
31+ callbacks = []
3032 self .callbacks = callbacks
3133
3234 def on (self , success = None , error = None ):
@@ -44,7 +46,7 @@ def on(self, success=None, error=None):
4446 :obj:`Effect` will be passed to the next callback.
4547 """
4648 return Effect (self .intent ,
47- callbacks = self .callbacks + (( success , error ),) )
49+ callbacks = self .callbacks + [( success , error )] )
4850
4951
5052class _Box (object ):
@@ -149,3 +151,20 @@ def _perform(bouncer, effect):
149151 _run_callbacks (bouncer , effect .callbacks , (True , e ))
150152
151153 trampoline (_perform , effect )
154+
155+
156+ def catch (exc_type , callable ):
157+ """
158+ A helper for handling errors of a specific type.
159+
160+ eff.on(error=catch(SpecificException,
161+ lambda exc_info: "got an error!"))
162+
163+ If any exception other than a ``SpecificException`` is thrown, it will be
164+ ignored by this handler and propogate further down the chain of callbacks.
165+ """
166+ def catcher (exc_info ):
167+ if isinstance (exc_info [1 ], exc_type ):
168+ return callable (exc_info )
169+ six .reraise (* exc_info )
170+ return catcher
0 commit comments