[Twisted-Python] problems with tracebacks

Mike Pelletier mike at mkp.ca
Thu Aug 17 08:27:27 EDT 2006

On Saturday 05 August 2006 12:09, Manlio Perillo wrote:
> Ok, thanks.
> But this means that I cannot use defer.fail to report an exception
> directly.
> As an example
> if a < 0:
>   return defer.fail(RuntimeError("must be non negative"))
> does not print a traceback.

Exception instances don't contain a traceback unless they have been raised.  
Below are three tests that return deferreds that result in a traceback and 
custom error message being printed.  Seems like there's probably a better 
way, but this has always been clear enough that I haven't looked for it.


"""The most perverse TestCase I've ever written.  Failure is an error,
an error is success, and success is failure.

from twisted.trial.unittest import TestCase
from twisted.python.failure import Failure
from twisted.internet.defer import Deferred, fail

class FailExample(TestCase):
    def test_implicitTBfromDeferred(self):
        d = Deferred()
            raise RuntimeError("Fear of success")
        return d

    def test_implicitTBfromFail(self):
            raise RuntimeError("This isn't happening")
            return fail()

    def test_explicitTBfromFailure(self):
        d = Deferred()
        f = None
            raise RuntimeError("Hair on fire")
            f = Failure()
        return d

More information about the Twisted-Python mailing list