Skip to content

Commit

Permalink
Explicitly reject empty response of requests
Browse files Browse the repository at this point in the history
Only notifications are expecting an empty response from the server.
  • Loading branch information
tcalmant committed Oct 27, 2022
1 parent 3422768 commit 081d103
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions jsonrpclib/jsonrpc.py
Expand Up @@ -628,7 +628,7 @@ def _request(self, methodname, params, rpcid=None):
config=self._config,
)
response = self._run_request(request)
check_for_errors(response)
check_for_errors(response, False)
return response["result"]

def _request_notify(self, methodname, params, rpcid=None):
Expand All @@ -649,7 +649,7 @@ def _request_notify(self, methodname, params, rpcid=None):
config=self._config,
)
response = self._run_request(request, notify=True)
check_for_errors(response)
check_for_errors(response, True)

def _run_request(self, request, notify=False):
"""
Expand Down Expand Up @@ -903,7 +903,7 @@ def __get_result(item):
Checks for error and returns the "real" result stored in a MultiCall
result.
"""
check_for_errors(item)
check_for_errors(item, False)
return item["result"]

def __iter__(self):
Expand Down Expand Up @@ -1349,20 +1349,24 @@ def loads(data, config=jsonrpclib.config.DEFAULT):
# ------------------------------------------------------------------------------


def check_for_errors(result):
def check_for_errors(result, expect_none):
"""
Checks if a result dictionary signals an error
:param result: A result dictionary
:param expect_none: If True, only accept ``None``, else reject it
:raise TypeError: Invalid parameter
:raise NotImplementedError: Unknown JSON-RPC version
:raise ValueError: Invalid dictionary content
:raise ProtocolError: An error occurred on the server side
:return: The result parameter
"""
if expect_none and result is None:
# Notification / expected None
return

if not result:
# Notification
return result
raise TypeError("Empty response.")

if not isinstance(result, utils.DictType):
# Invalid argument
Expand Down

0 comments on commit 081d103

Please sign in to comment.