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

Glyph Lefkowitz glyph at twistedmatrix.com
Wed Sep 29 19:31:29 EDT 2010

On Sep 29, 2010, at 6:56 PM, Chris Withers wrote:

> On 29/09/2010 22:06, exarkun at twistedmatrix.com wrote:
>>> That's one side of things, sure, but how can I write a scheduler which
>>> handles the current situation?
>> Beats me.
> So, if some buggy code that should be doing a deferred callback/errback 
> instead raises an exception, you're basically screwed?
> There's really no way to write a "safety belt" handler that will record 
> the problem but then keep going?!
> Chris

There are so many ways.

You can add a log observer that handles isError=True log messages.

You can write a 'safetyBelt' function that catches the exception and does something with it.

You can always invoke your code with maybeDeferred, which will turn exceptions into failures for you.

You can use inlineCallbacks, where in 'x = yield y()', y() raising an exception and y() returning a failed Deferred are basically indistinguishable to the caller.

And there are hundreds of subtle variations on each of these approaches.

Why would you say there's no way?

More information about the Twisted-Python mailing list