[Twisted-Python] Transforming Exceptions for use with PB

Brian Granger bgranger at scu.edu
Wed Apr 12 01:30:13 MDT 2006


Hi,

I have a question about how Exceptions are handled by PB.

I am using Twisted to expose a python object (wrapped into a Twisted
Service) over a couple of network protocols (PB and XML-RPC).  I am
using interfaces/components/adapters to adapt my service into entities
that each protocol can use:

class IPerspectiveFunction(Interface):

    def remote_function(self, x):

class PerspectiveFunctionFromService(pb.Root):

    implements(IPerspectiveFunction)

    def __init__(self, service):
        self.service = service

    def remote_function(self, x):
        return self.service.function(x)     # The service returns a Deferred

self.service.function returns a Deferred, whose errbacks are fired
when an Exception is raised in self.service.function.

Here is my dilemma:

- The code in self.service.function that raises exceptions is in
another library that doesn't know about Twisted.  Thus the exceptions
are not sublcasses of pb.Error.

- Even if I did have control over these exceptions, I wouldn't want to
subclass pb.Error as I need to propagate the errors out to other (not
PB) protocols.

- I would like to detect certain types of exceptions (maybe using
trap()) and convert them to instances of pb.Error (or a subclass) so I
can send them over the line using PB.

The following almost works:

    def remote_function(self, x):
        d = self.service.function(x)     # The service returns a Deferred
        def transformFailure(f):
            return failure.Failure(pb.Error(f.printTraceback()))
        d.addErrback(transformFailure)
        return d

The other side gets the Error just fine, but the PB server still
prints the traceback and says "Peer will receive following PB
traceback:"  Why does PB handle it this way, when the Failure instance
that PB gets contains a pb.Error exception?

Any thoughts on how to transform general Failure instances to ones
that can be handled in a clean way by PB?  I guess there is amore
general question of how to transform Failure instances into things
that can be handled by various network protocols (such as XML-RPC or
even HTTP)

Thanks in advance.

Brian


Brian Granger
Santa Clara University
ellisonbg at gmail.com




More information about the Twisted-Python mailing list