[Twisted-Python] Newbie question about error handling.

I'm working on a client/server twisted app based on LineReceiver, and I'd like to implement better error-handling. The code for the server looks something like this: class MyServer(LineReceiver): def lineReceived(self, line): pass # stuff happens def rawDataReceived(self, data): pass # other stuff happens # maybe some other methods too factory = Factory() factory.protocol = MyServer reactor.listenTCP(8000, factory) reactor.run() 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. Is something like this possible? I'm still pretty new to Twisted, so I don't know all the idioms yet.

On Jan 18, 2008 4:23 PM, Brad Smith <bnsmith@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

You've given me two really good ideas to mull over. Of course, the decorator method would work. It was like a flash of light as soon as I read it... but now I'm not sure if I should... Thanks for the help. On Jan 18, 2008 6:03 PM, Drew Smathers <drew.smathers@gmail.com> wrote:
On Jan 18, 2008 4:23 PM, Brad Smith <bnsmith@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 _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Brad Smith
-
Drew Smathers