Skip to content

Commit

Permalink
Opt for logging user_message and response on RPC errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Apr 24, 2024
1 parent f7d86b8 commit ebcad6e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
14 changes: 4 additions & 10 deletions tests/core/manager/test_response_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
METHOD_UNAVAILABLE_MSG = (
"the method eth_getTransactionByHash does not exist/is not available."
)
DEFAULT_USER_MSG = (
"An RPC error was returned by the node. Check the message provided in the error "
"and any available logs for more information."
)


VALID_RESULT_OBJ_RESPONSE = {"jsonrpc": "2.0", "id": 1, "result": {"foo": "bar"}}
Expand Down Expand Up @@ -179,19 +175,17 @@ def test_formatted_response_invalid_response_object(w3, response, error, error_m
(
VALID_ERROR_RESPONSE,
Web3RPCError,
f'{VALID_ERROR_RESPONSE["error"]}\nUser message: {DEFAULT_USER_MSG}',
f'{VALID_ERROR_RESPONSE["error"]}',
),
(
ERROR_RESPONSE_VALID_ID_STRING,
Web3RPCError,
f'{ERROR_RESPONSE_VALID_ID_STRING["error"]}\n'
f"User message: {DEFAULT_USER_MSG}",
f'{ERROR_RESPONSE_VALID_ID_STRING["error"]}',
),
(
ERROR_RESPONSE_VALID_ID_NONE,
Web3RPCError,
f'{ERROR_RESPONSE_VALID_ID_NONE["error"]}\n'
f"User message: {DEFAULT_USER_MSG}",
f'{ERROR_RESPONSE_VALID_ID_NONE["error"]}',
),
(
ERROR_RESPONSE_VALID_METHOD_UNAVAILABLE,
Expand Down Expand Up @@ -284,7 +278,7 @@ def test_formatted_response_invalid_error_object(
identity,
raise_block_not_found,
Web3RPCError,
f'{VALID_ERROR_RESPONSE["error"]}\nUser message: {DEFAULT_USER_MSG}',
f'{VALID_ERROR_RESPONSE["error"]}',
),
),
)
Expand Down
6 changes: 0 additions & 6 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def __init__(
# Assign properties of Web3Exception
self.user_message = user_message

def __str__(self) -> str:
# append a clarifying user message if one is provided
return super().__str__() + (
f"\nUser message: {self.user_message}" if self.user_message else ""
)


class Web3AssertionError(Web3Exception, AssertionError):
"""
Expand Down
22 changes: 17 additions & 5 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def _validate_response(
response: RPCResponse,
error_formatters: Optional[Callable[..., Any]],
is_subscription_response: bool = False,
logger: Optional[logging.Logger] = None,
) -> None:
if "jsonrpc" not in response or response["jsonrpc"] != "2.0":
_raise_bad_response_format(
Expand Down Expand Up @@ -191,12 +192,18 @@ def _validate_response(
response, 'error["code"] is required and must be an integer value.'
)
elif code == METHOD_NOT_FOUND:
raise MethodUnavailable(
exception = MethodUnavailable(
repr(error),
rpc_response=response,
user_message="Check your node provider or your client's API docs to "
"see what methods are supported and / or currently enabled.",
user_message=(
"This method is not available. Check your node provider or your "
"client's API docs to see what methods are supported and / or "
"currently enabled."
),
)
logger.error(exception.user_message)
logger.debug(f"RPC error response: {response}")
raise exception

# errors must include a message
error_message = error.get("message")
Expand All @@ -206,7 +213,11 @@ def _validate_response(
)

apply_error_formatters(error_formatters, response)
raise Web3RPCError(repr(error), rpc_response=response)

web3_rpc_error = Web3RPCError(repr(error), rpc_response=response)
logger.error(web3_rpc_error.user_message)
logger.debug(f"RPC error response: {response}")
raise web3_rpc_error

elif "result" not in response and not is_subscription_response:
_raise_bad_response_format(response)
Expand Down Expand Up @@ -298,8 +309,8 @@ async def _coro_make_request(
#
# See also: https://www.jsonrpc.org/specification
#
@staticmethod
def formatted_response(
self,
response: RPCResponse,
params: Any,
error_formatters: Optional[Callable[..., Any]] = None,
Expand All @@ -316,6 +327,7 @@ def formatted_response(
response,
error_formatters,
is_subscription_response=is_subscription_response,
logger=self.logger,
)

# format results
Expand Down

0 comments on commit ebcad6e

Please sign in to comment.