[Twisted-Python] failures, errbacks and exception handling
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.
On Tue, Apr 8, 2008 at 2:40 PM, Atilla <theatilla@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
On Tue, Apr 8, 2008 at 2:18 PM, Drew Smathers <drew.smathers@gmail.com> wrote:
def crash(reason): log.err('ERROR: %s' % reason) ... Do the rollback or whatever else ... reason.printTraceback()
Just a note on the rollback: if there's an error, the adbapi run* methods roll the transaction back before sending the error to the errback -- so I don't know of anything left to rollback in the errback. ~ Nathan
On 09/04/2008, Nathan <nathan.stocks@gmail.com> wrote:
On Tue, Apr 8, 2008 at 2:18 PM, Drew Smathers <drew.smathers@gmail.com> wrote:
def crash(reason): log.err('ERROR: %s' % reason) ... Do the rollback or whatever else ... reason.printTraceback()
Just a note on the rollback: if there's an error, the adbapi run* methods roll the transaction back before sending the error to the errback -- so I don't know of anything left to rollback in the errback.
~ Nathan
Yes, there's no need to rollback anything. I just want to attempt a retry on the transaction, in cases of lock timeouts or deadlocks, since there'll be the possibility of running clashing transactions at the same time. I still can't figure out how to make failure.trap() to work correctly for me however. I'm trying to trap a mysqldb Operational exception, but although the error is not further propagated after the trap call, I get nothng in the return value.
On Wed, Apr 9, 2008 at 2:20 AM, Atilla <theatilla@gmail.com> wrote:
I still can't figure out how to make failure.trap() to work correctly for me however. I'm trying to trap a mysqldb Operational exception, but although the error is not further propagated after the trap call, I get nothng in the return value.
I'm only two-months into twisted myself, and have never used trap(), but from reading the api docs here: http://twistedmatrix.com/documents/current/api/twisted.python.failure.Failur... ...it would seem that calling trap causes it to immediately raise-to-the-next-errback anything you _don't_ trap. Perhaps that's the problem you're experiencing? (Just a stab in the dark) ~ Nathan
participants (3)
-
Atilla -
Drew Smathers -
Nathan