views:

188

answers:

1

I'm trying to pass arbitrary exceptions from a XMLRPC server to a client (both Python scripts, exception types are defined on both sides). There's an exemplary client-side implementation at ActiveState Recipes which parses the returned "faultString", compares it with a list of known exceptions and, if found, raises that exception (instead of wrapping it in a xmlrpclib.Fault).

Example of an XMLRPC exception response:

<?xml version='1.0'?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value>
            <int>1</int>
          </value>
        </member>
        <member>
          <name>faultString</name>
          <value>
            <string>ValueError:</string>
          </value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>

The ActiveState recipe splits the "ValueError:" string and compares "ValueError" with ValueError.__name__ (which is in allowed_errors = [ValueError, TypeError], cf. link above).

Is it possible to pass all exceptions like this, i.e. automatically raise any exception (derived from Exception or some other base class) on the client side (after it was raised on the server)?

The ActiveState script works, but I don't want to register every single exception that could be thrown (must be in allowed_errors).

(Bonus question: Is there another technology apart from XMLRPC that could handle this problem properly? Pyro?)

+1  A: 

You could populate the allowed_errors list from __builtins__:

[exc for exc in __builtins__ if isinstance(exc, BaseException)]

This would handle the common case, for built-in exceptions like ValueError, TypeError, OSError, etc. You could probably do something more advanced like PyYAML and pickle, where they automatically hunt down whatever referenced exception gets sent, but this opens yourself up to malicious attacks.

If you want the kind of client/server transparency that automatically raising exceptions from the server on the client implies, Pyro would probably be a better choice than XML-RPC.

LeafStorm
I think the first suggestion won't work as I have lots of custom exception types. Maybe I'll try out Pyro in the next days.
AndiDog