[Twisted-Python] Newbie question about error handling.

Drew Smathers drew.smathers at gmail.com
Fri Jan 18 18:03:39 EST 2008


On Jan 18, 2008 4:23 PM, Brad Smith <bnsmith at gmail.com> wrote:

> Suppose that an exception is thrown in lineReceived or
> rawDataReceived, maybe just something as simple as a naming error
> caused by a misspelled variable name. Is there any way to implement a
> default exception handler that would allow me to send something back
> to the client before the protocol dies? It would be nice to send the
> exception traceback to the client to help figure out what went wrong.
> I suppose I could wrap all of the methods in the class in a try/catch
> block, but that would be a lot of wrapping to do. I'd prefer to be
> able to do something like adding a 'defaultExceptionHandler' method to
> the LineReceiver derived class that would be called by the reactor.
>

It will save you more time to fix such a critical bug as a naming error
rather than build a protocol to only tell the client the server is broken.
However to answer your question, no.

Remember from the Zen of Python: "explicit is better than implicit" so to
handle legitimate errors you should define what exceptions are likely to
occur and incorporate these in your protocol design for error feedback to
the client.  If you want some kill-all exception handler for all your
protocols, consider writing a decorator:

@sendTraceback
def lineReceived(self, line):
    pass # do stuff


-- 
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/  \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\  /\\\ \\
/ /\\\  /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
              d.p.s
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080118/f200a265/attachment.htm 


More information about the Twisted-Python mailing list