[Twisted-Python] How to show traceback of "Unhandled error"?

Hi, Running the following script prints "Unhandled Error", but doesn't contain the traceback of where the exception is raised. In a massive codebase, it's hard to locate where is the malfunctioning code is. from twisted.internet.defer import Deferred def raiseErr(what): raise Exception(what) d = Deferred() d.addCallback(raiseErr) d.callback("asdf") # how to show the traceback without manually adding an errback at the end? # adding at the *end* of a deferred is hard as we don't know when the client stops adding callbacks. -- Best, Shiyao

This is an unfortunate implementation detail that you can address by starting the logging system. Try this: from twisted.logger import globalLogBeginner, textFileLogObserver from twisted.internet.defer import Deferred import sys globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)]) def raiseErr(what): raise Exception(what) d = Deferred() d.addCallback(raiseErr) d.callback("asdf") You should see a traceback. For future reference, the important lines are these: from twisted.logger import globalLogBeginner, textFileLogObserver import sys globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)]) They should be executed before any code that uses Deferreds. twistd and the newer twist Twisted application runners automatically run these for you before starting your Service. This is a great reason to write Twisted application plugins: https://twistedmatrix.com/documents/current/core/howto/tap.html Of course it would be better to not have to do anything at all to see important information related to errors; the default behavior of Deferred makes things harder to understand than they have to be. Fortunately, good progress has been made on this issue: https://twistedmatrix.com/trac/ticket/9333 The associated PR needs a careful review to ensure its handling of unicode is correct. Any takers? On Fri, May 18, 2018, at 1:56 AM, Shiyao MA wrote:
-- Mark Williams mrw@enotuniq.org

You'll get more detailed info: CRITICAL:twisted:Unhandled error in Deferred: CRITICAL:twisted:(debug: C: Deferred was created: C: File "foo.py", line 15, in <module> C: d = defer.Deferred() I: First Invoker was: I: File "foo.py", line 17, in <module> I: d.callback("asdf") ) Traceback (most recent call last): File "/home/jlitzing/dev/twisted/twisted-fork/src/twisted/internet/defer.py", line 653, in _runCallbacks current.result = callback(current.result, *args, **kw) File "foo.py", line 13, in raiseErr raise Exception(what) Exception: asdf Cheers, -Jason

This is an unfortunate implementation detail that you can address by starting the logging system. Try this: from twisted.logger import globalLogBeginner, textFileLogObserver from twisted.internet.defer import Deferred import sys globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)]) def raiseErr(what): raise Exception(what) d = Deferred() d.addCallback(raiseErr) d.callback("asdf") You should see a traceback. For future reference, the important lines are these: from twisted.logger import globalLogBeginner, textFileLogObserver import sys globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)]) They should be executed before any code that uses Deferreds. twistd and the newer twist Twisted application runners automatically run these for you before starting your Service. This is a great reason to write Twisted application plugins: https://twistedmatrix.com/documents/current/core/howto/tap.html Of course it would be better to not have to do anything at all to see important information related to errors; the default behavior of Deferred makes things harder to understand than they have to be. Fortunately, good progress has been made on this issue: https://twistedmatrix.com/trac/ticket/9333 The associated PR needs a careful review to ensure its handling of unicode is correct. Any takers? On Fri, May 18, 2018, at 1:56 AM, Shiyao MA wrote:
-- Mark Williams mrw@enotuniq.org

You'll get more detailed info: CRITICAL:twisted:Unhandled error in Deferred: CRITICAL:twisted:(debug: C: Deferred was created: C: File "foo.py", line 15, in <module> C: d = defer.Deferred() I: First Invoker was: I: File "foo.py", line 17, in <module> I: d.callback("asdf") ) Traceback (most recent call last): File "/home/jlitzing/dev/twisted/twisted-fork/src/twisted/internet/defer.py", line 653, in _runCallbacks current.result = callback(current.result, *args, **kw) File "foo.py", line 13, in raiseErr raise Exception(what) Exception: asdf Cheers, -Jason
participants (3)
-
Jason Litzinger
-
Mark Williams
-
Shiyao MA