[Twisted-Python] Wrapping functions that return deferreds
Hi, I have a library that has a call() method, which returns a Deferred. My application used to just use .addCallback() but obviously that doesn't have any error handling. I then switched to .addCallbacks() so that I could call a common error handler (to pop up a dialog) but that got tedious to add to every invocation so I thought I'd be cunning and wrap the call() method. def wrap_deferred (func): def wrapped(*args, **kwargs): d = func (*args, **kwargs) d.addErrback (handle_twisted_error) return d return wrapped # flickr is the object, call is the method I'm overriding flickr.call = wrap_deferred (flickr.call) # Then later in my app flickr.call(foo).addCallback(handleFoo) However, this doesn't work: I get the standard unhandled error in deferred handling. Can anyone spot the mistake I'm making? Thanks, Ross -- Ross Burton mail: ross@burtonini.com jabber: ross@burtonini.com www: http://www.burtonini.com./ PGP Fingerprint: 1A21 F5B0 D8D0 CFE3 81D4 E25A 2D09 E447 D0B4 33DF
Le mercredi 11 avril 2007 à 10:42 +0100, Ross Burton a écrit :
def wrap_deferred (func): def wrapped(*args, **kwargs): d = func (*args, **kwargs) d.addErrback (handle_twisted_error) return d return wrapped
# flickr is the object, call is the method I'm overriding flickr.call = wrap_deferred (flickr.call)
# Then later in my app flickr.call(foo).addCallback(handleFoo)
Since you add the errback before adding the callback, errors happening in handleFoo will not get caught by the errback.
On Wed, 2007-04-11 at 12:05 +0200, Antoine Pitrou wrote:
Le mercredi 11 avril 2007 à 10:42 +0100, Ross Burton a écrit :
def wrap_deferred (func): def wrapped(*args, **kwargs): d = func (*args, **kwargs) d.addErrback (handle_twisted_error) return d return wrapped
# flickr is the object, call is the method I'm overriding flickr.call = wrap_deferred (flickr.call)
# Then later in my app flickr.call(foo).addCallback(handleFoo)
Since you add the errback before adding the callback, errors happening in handleFoo will not get caught by the errback.
That is fine -- handleFoo doesn't throw any errors. The deferred returned by the original call() is calling errBack(). Ross -- Ross Burton mail: ross@burtonini.com jabber: ross@burtonini.com www: http://www.burtonini.com./ PGP Fingerprint: 1A21 F5B0 D8D0 CFE3 81D4 E25A 2D09 E447 D0B4 33DF
participants (2)
-
Antoine Pitrou -
Ross Burton