Re: How to show traceback of "Unhandled error"?

Mark Williams <[email protected]>
Newsgroups gmane.comp.python.twisted
Message-ID <1526698393.297902.1377455416.405F6E2B@webmail.messagingengine.com>
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
> [email protected]
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


-- 
  Mark Williams
  [email protected]

_______________________________________________
Twisted-Python mailing list
[email protected]
https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.