aiothrift icon indicating copy to clipboard operation
aiothrift copied to clipboard

Exceptions aren't propagated from server to client

Open vaastav opened this issue 4 years ago • 2 comments

If the server raises an exception then those exceptions aren't propagated to the client. The normal thrift library automatically handles this by sending a ThriftApplicationException from the server to the client as part of the response message.

How can I propagate exceptions from the server to the client using aiothrift? Any help would be appreciated.

vaastav avatar Sep 22 '21 09:09 vaastav

Instead of an application exception on the client-side, I get an IncompleteReadError exception followed by a ConnectionClosedError

Traceback (most recent call last): File "/home/vaastav/anaconda3/lib/python3.8/site-packages/aiothrift/connection.py", line 119, in execute result = await self._recv(api) File "/home/vaastav/anaconda3/lib/python3.8/site-packages/aiothrift/connection.py", line 136, in _recv fname, mtype, rseqid = await self._iprot.read_message_begin() File "/home/vaastav/anaconda3/lib/python3.8/site-packages/aiothrift/protocol.py", line 469, in read_message_begin api, ttype, seqid = await read_message_begin( File "/home/vaastav/anaconda3/lib/python3.8/site-packages/aiothrift/protocol.py", line 159, in read_message_begin data = await reader.readexactly(4) File "/home/vaastav/anaconda3/lib/python3.8/asyncio/streams.py", line 721, in readexactly raise exceptions.IncompleteReadError(incomplete, n) asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 4 expected bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "client.py", line 16, in asyncio.run(create_connection()) File "/home/vaastav/anaconda3/lib/python3.8/asyncio/runners.py", line 43, in run return loop.run_until_complete(main) File "/home/vaastav/anaconda3/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete return future.result() File "client.py", line 12, in create_connection print(await conn.add(5, 6)) File "/home/vaastav/anaconda3/lib/python3.8/site-packages/aiothrift/connection.py", line 130, in execute raise ConnectionClosedError("Server connection has closed") from e aiothrift.errors.ConnectionClosedError: Server connection has closed

vaastav avatar Sep 22 '21 09:09 vaastav

I'm not sure if it's appropriate to raise some kind of ThriftApplicationException. Maybe we could hijack the server exception and manually create a thrift exception and send that back to client, I just wonder if it's the best way to do it.

I think the most simple solution is to define an exception struct in your thrift file and throws that exception in your Service like

service PingPong {
    string ping() throws (1:SampleException error),

}
exception SampleException {
    1: list<string> failed
}

ryanwang520 avatar Apr 17 '22 16:04 ryanwang520