How to deal with custom exception using Pyro4?

450 Views Asked by At

I'm trying to expose my classes using Pyro4. The problem is that my class has a custom exception that Pyro4 is not able to handle it.

I have tried using serialization but nothing seems to work.

What I have is:

# exception class
class TestException(Expection):
    pass


# test class
@Pyro4.expose
class Test:

   def get_exception(self):
       raise TestException("This is a random exception.")

Pyro4 is not able to raise the TestException. It give me an error: {SerializedError}unsupported serialized class: TestException

Does anyone have an idea on how to fix this?

Thanks.

1

There are 1 best solutions below

0
On

You'll need to teach Pyro (or rather, the underlying Serpent serializer) that you agree to deserialize your custom exception class, and how it must do that. Depending on this, you may also have to make sure both parties (client and server) have access to the same source module that defines your TestException class.

This is in the documentation. Specifically, https://pyro4.readthedocs.io/en/stable/clientcode.html#changing-the-way-your-custom-classes-are-de-serialized

It is for security reasons that this raises an error and requires programmer effort. (Deserializing random classes that you receive from the network is dangerous)

In the end it may be easier to just catch the exception in your Pyro server and raise one of the standard Python exceptions instead.