Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't swallow server-side exceptions #35

Merged
merged 5 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tests/functional/single_server/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ async def divide(semaphore, req):

def test_server_error():
client = ZeroClient(server.HOST, server.PORT)
msg = client.call("error", "some error")
assert msg is None
try:
msg = client.call("error", "some error")
raise AssertionError("Should have thrown an Exception")
except zero.error.RemoteException:
pass


def test_default_timeout():
Expand Down
14 changes: 9 additions & 5 deletions zero/client_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

from zero import config
from zero.encoder import Encoder, get_encoder
from zero.error import MethodNotFoundException, TimeoutException
from zero.error import MethodNotFoundException, TimeoutException, RemoteException
from zero.utils.util import current_time_us, unique_id
from zero.zero_mq import AsyncZeroMQClient, ZeroMQClient, get_async_client, get_client
from zero.zero_mq.helpers import zpipe_async

def check_response(resp_data):
if isinstance(resp_data, dict):
if e := resp_data.get("__zerror__method_not_found"):
raise MethodNotFoundException(e)
if e := resp_data.get("__zerror__server_exception"):
raise RemoteException(e)

class ZeroClient:
def __init__(
Expand Down Expand Up @@ -101,8 +107,7 @@ def _poll_data():
while resp_id != req_id:
resp_id, resp_data = _poll_data()

if isinstance(resp_data, dict) and "__zerror__method_not_found" in resp_data:
raise MethodNotFoundException(resp_data.get("__zerror__method_not_found"))
check_response(resp_data)

return resp_data

Expand Down Expand Up @@ -237,7 +242,6 @@ async def _poll_data():

resp_data = self._resp_map.pop(req_id)

if isinstance(resp_data, dict) and "__zerror__method_not_found" in resp_data:
raise MethodNotFoundException(resp_data.get("__zerror__method_not_found"))
check_response(resp_data)

return resp_data
1 change: 1 addition & 0 deletions zero/client_server/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def handle_msg(self, rpc, msg):

except Exception as exc:
logging.exception(exc)
ret = {"__zerror__server_exception": repr(exc)}

return ret

Expand Down
3 changes: 3 additions & 0 deletions zero/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ class TimeoutException(ZeroException):

class ConnectionException(ZeroException):
pass

class RemoteException(Exception):
pass