[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.

Mike.

----
"""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()
        try:
            raise RuntimeError("Fear of success")
        except:
            d.errback()
        return d

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

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





More information about the Twisted-Python mailing list