3
# Copyright (c) 2009 Twisted Matrix Laboratories.
4
# See LICENSE for details.
6
from twisted.internet import defer
7
from twisted.python import failure, util
10
This example is analogous to a function calling .errback(failure)
14
class Counter(object):
19
print "we got an exception: %s" % (f.getTraceback(),)
22
def handleResult(result):
24
print "callback %s" % (Counter.num,)
25
print "\tgot result: %s" % (result,)
26
return "yay! handleResult was successful!"
28
def failAtHandlingResult(result):
30
print "callback %s" % (Counter.num,)
31
print "\tgot result: %s" % (result,)
32
print "\tabout to raise exception"
33
raise RuntimeError, "whoops! we encountered an error"
36
def behindTheScenes(result):
37
if not isinstance(result, failure.Failure): # ---- callback
39
result = handleResult(result)
41
result = failure.Failure()
46
if not isinstance(result, failure.Failure): # ---- callback
50
result = handleFailure(result)
52
result = failure.Failure()
55
def deferredExample(result):
57
d.addCallback(handleResult)
58
d.addCallback(failAtHandlingResult)
59
d.addErrback(handleFailure)
64
if __name__ == '__main__':
67
raise RuntimeError, "*doh*! failure!"
69
result = failure.Failure()
70
behindTheScenes(result)
71
print "\n-------------------------------------------------\n"
73
deferredExample(result)