Here's code that generates an error on line #9 in f1()
1 import time, sys
2 from twisted.internet import reactor
3 from twisted.internet.task import deferLater
4 from twisted.internet.defer import inlineCallbacks
5
6 @inlineCallbacks
7 def f1():
8 yield time.time()
9 {'a': 'A', 'b': 'B'}['c'] # KeyError
10
11 @inlineCallbacks
12 def f2():
13 yield f1()
14
15 def on_err(failure):
16 print("on_err: ===>", failure.getErrorMessage())
17 failure.printTraceback(file=sys.stderr)
18 if reactor.running:
19 reactor.stop()
20
21 def done(*args, **kargs):
22 print("done.")
23 reactor.stop()
24
25 deferLater(reactor, 0, f2).addCallback(done).addErrback(on_err)
26 reactor.run()
running w/ python 2.7 outputs this:
--- <exception caught here> ---
File ".....\lib\site-packages\twisted\internet\defer.py", line 1128, in _inlineCallbacks
result = g.send(result)
File "../temp/tb.py", line 9, in f1
{'a': 'A', 'b': 'B'}['c']
exceptions.KeyError: 'c'
python 3.6.1 (twisted 17.5.0) outputs this:
File ".....\lib\site-packages\twisted\internet\defer.py", line 1299, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File ".....\lib\site-packages\twisted\python\failure.py", line 393, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "../temp/tb.py", line 13, in f2
yield f1()
builtins.KeyError: 'c'
I can know there is a failure in f1() but line and other specifics are dropped
changing line #25 to call f1() gives a better message, so it's something related to stack frame.
Is there any way (aside from adding logging at each level to internet/defer.py:_inlineCallbacks) to get a better error message in py3?
thank you
- Aaron