mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.18k stars 186 forks source link

RPCs: Exceptions are serialized to the client #502

Closed dvf closed 1 year ago

dvf commented 1 year ago

Using the RPC module, I've noticed that Exceptions are being swallowed and serialized and propagated to the caller. Is there any way to have the exception also appear in the output for the host (where the RPC lives)?

mosquito commented 1 year ago

@dvf in the pickle-based RPC an exception might be serialised and deserialised as well (see this test for example). Since the pickle is not safe to use in an untrusted environment, there are alternative implementations using JsonRPC which raises the special exception JsonRPCError.

Could you please provide your code sample for double-check this?

dvf commented 1 year ago

Thanks for the quick response @mosquito. I think I mis-described my issue.

I have a client calling a host and when the host raises in exception I'd like it to appear in the logs for the host, but it looks like the exception is being serialized and sent back to the client. Is there any way to raise the exception (and see it) on the host?

dvf commented 1 year ago

For anyone in a similar position, I ended up doing this:

from aio_pika.patterns import JsonRPC

class JsonRPCWithLocalExceptions(JsonRPC):
    def serialize_exception(self, exception: Exception) -> Any:
        # Log locally
        logger.exception(exception)
        return {
            "error": {
                "type": exception.__class__.__name__,
                "message": repr(exception),
                "args": exception.args,
            },
        }
async def foo():
    rpc = await JsonRPCWithLocalExceptions.create(channel)
    ...