[Twisted-Python] how to write a safe catch-all

Chris Withers chris at simplistix.co.uk
Thu Sep 30 11:19:23 EDT 2010

On 30/09/2010 15:33, exarkun at twistedmatrix.com wrote:
>> 2010-09-30 15:07:03,161 ERROR   : log         (22194|7f41910b26e0):
>> Unhandled Error
>> Traceback (most recent call last):
>>    File "test_looping.py", line 47, in<module>
>>      reactor.run()
>>    File "/twisted/internet/base.py", line 1166, in run
>>      self.mainLoop()
>>    File "/twisted/internet/base.py", line 1175, in mainLoop
>>      self.runUntilCurrent()
>> ---<exception caught here>  ---
>>    File "/twisted/internet/base.py", line 779, in runUntilCurrent
>>      call.func(*call.args, **call.kw)
>>    File "test_looping.py", line 30, in __call__
>>      del self.connector
>> exceptions.AttributeError: Break instance has no attribute 'connector'
> This is not logged by your code.  Do you recognize that?

Yes, this is what I'm complaining about ;-)

>> /twisted/internet/defer.py:262: DeprecationWarning: Don't pass strings
>> (like 'Break!') to failure.Failure (replacing with a DefaultException).
>>    fail = failure.Failure(fail)
>> 2010-09-30 15:07:05,167 ERROR   : log         (22194|7f41910b26e0):
>> Unhandled scheduled exception
>> Traceback (most recent call last):
>> Failure: twisted.python.failure.DefaultException: Break!
> This comes from some code not included in the code you posted.

Sure it is, it was attached to the message I sent at 14:36.

> It looks
> like you're using Failure wrong though.

Sure, but that's hardly the issue at hand here...
What should I be passing to errback?

>> So, how come my log.err doesn't get used for the AttributeError on
>> connector?
> Your Deferred *never* fires with a Failure corresponding to that
> AttributeError.  This is the most important thing.  If you don't
> understand this, say so and we can talk about it some more.  Everything
> else is just confusing particulars.

Yes, I understand this, and this is what I'm talking about when I say "I 
cannot gracefully handle the exception." Reading back, yes, it appears I 
was mistaken at some stage that my `loop` function was handling the 
exception, but I do understand now that it was not... which is 

> The AttributeError never becomes an error result on any Deferred.  It is
> caught inside the reactor implementation, logged there by Twisted
> itself, and then thrown away forever.



More information about the Twisted-Python mailing list