[Twisted-Python] failures, errbacks and exception handling

Drew Smathers drew.smathers at gmail.com
Tue Apr 8 14:18:30 MDT 2008


On Tue, Apr 8, 2008 at 2:40 PM, Atilla <theatilla at gmail.com> wrote:
> Im a little confused when it comes to handling some errors in twisted.
>  More specifically - when it comes to processing failures that are
>  raised in adbapi.
>
>  Now - when runInteraction succeeds, all is ok and things are commited,
>  that's nice and clean. When it raises an error, I can do two things -
>  either wrap my calls inside the callable passes to runInteraction in
>  Try:Except, or attach an errback.
>
>  I'm not exactly sure which one to take. I only need to catch specific
>  exceptions - Mysql OperationalError (timeouts and such, in case I want
>  to retry). Everything else is critical and should log & abort.
>
>  The way I understood it, I should attach an errback where I call
>  trap() on the passed Failure. So I'd do like
>
>  handleError(falure):
>   error = failure.trap(OperationalError)
>   if error:
>     log.err(error)
>
>  myCall.addErrback(handleError)
>
>  As far as I get it, if the error is different (say, syntax one) it'll
>  be propagated. However afteri add the trap call() nothing else gets
>  past that handleError, no Unhandled Errors are logged, and my error
>  object contains nothing. I wonder what am I missing here.
>

That really depends on what you want to do.  If you just want to log
the exception (IndexError, etc) that you're not trapping, consider
creating a last ditch error handler like:

def crash(reason):
    log.err('ERROR: %s' % reason)
    ... Do the rollback or whatever else ...
    reason.printTraceback()

And add the above to your errback chain:

myCall.addErrback(handleError).addErrback(crash)


-- 
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
 d.p.s




More information about the Twisted-Python mailing list