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

Mark Williams mrw at enotuniq.org
Fri May 18 20:53:13 MDT 2018


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:
> 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
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


-- 
  Mark Williams
  mrw at enotuniq.org




More information about the Twisted-Python mailing list