My problem: I have a deferred with callbacks and errbacks. I need to stop the process after a specific errback. In another words, if a specific function of errback is called, i need to get its returns and DO NOT process the following callbacks.
from twisted.internet import defer
from twisted.python import failure, util
class Test (object):
@classmethod
def handleFailure(self, f):
print "handleFailure"
f.trap(RuntimeError)
return '0', 'erro'
@classmethod
def handleResult(self, result, message):
print "handleResult of %s. Old results %s, %s: " % (message, result[0], result[1])
return 1, 'ok'
@classmethod
def doFailure (self, result, message):
print "handleResult of %s. Old results %s, %s: " % (message, result[0], result[1])
raise RuntimeError, "whoops! we encountered an error"
@classmethod
def deferredExample(self):
d = defer.Deferred()
# 1o. call without error
d.addCallback(Test.handleResult, 'call 1')
# 2o. call without error
d.addCallback(Test.handleResult, 'call 1')
# 3o. call causes the failure
d.addCallback(Test.doFailure, 'call 3')
# the failure calls the error back
d.addErrback (Test.handleFailure) # - A -
# after error back, the next call back is called
d.addCallback(Test.handleResult, 'call 4') # - B -
# and the last call back is called
d.addCallback(Test.handleResult, 'call 5') # - C -
d.callback("success")
return d.result[0], d.result[1]
if __name__ == '__main__':
# behindTheScenes("success")
print "\n-------------------------------------------------\n"
global num; num = 0
tst = Test()
rtn1, rtn2 = tst.deferredExample()
print "RTN: %s %s" % (rtn1, rtn2)
This code is a simple version than reflects what I need. After -A- process, I need to baypass -B- and -C-, and at the end, the response be "0, erro", not "1, ok'.
My current return:
handleResult of call 1. Old results s, u:
handleResult of call 1. Old results 1, ok:
handleResult of call 3. Old results 1, ok:
handleFailure
handleResult of call 4. Old results 0, erro:
handleResult of call 5. Old results 1, ok:
RTN: 1 ok
and I want:
handleResult of call 1. Old results s, u:
handleResult of call 1. Old results 1, ok:
handleResult of call 3. Old results 1, ok:
handleFailure
RTN: 0 erro
How can I do this?
Thanks in advance