[Twisted-Python] Newbie question about error handling.

Brad Smith bnsmith at gmail.com
Fri Jan 18 16:23:24 EST 2008

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)

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.

More information about the Twisted-Python mailing list