Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reedsa committed Sep 12, 2023
1 parent 9ce2461 commit 117b5cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
42 changes: 41 additions & 1 deletion tests/core/manager/test_response_formatters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import re

from eth_utils.toolz import (
identity,
Expand Down Expand Up @@ -41,6 +42,21 @@
"available",
},
}
INVALID_CODE_RESP_FORMAT = {
"jsonrpc": "2.0",
"error": {
"code": "-32601",
"message": "the method eth_getTransactionByHash does not exist/is not "
"available",
},
}
INVALID_MESSAGE_RESP_FORMAT = {
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": {},
},
}
ETH_TESTER_METHOD_NOT_FOUND_RESP_FORMAT = {
"error": "the method eth_getTransactionByHash does not exist/is not available",
}
Expand Down Expand Up @@ -175,7 +191,7 @@ def test_formatted_response_raises_errors(
identity,
identity,
BadResponseFormat,
f"The response was in an unexpected format and unable to be parsed. The error is: {UNEXPECTED_RESPONSE_FORMAT} is not valid under any of the given schemas. The raw response is: {UNEXPECTED_RESPONSE_FORMAT}", # noqa: E501
f"The response was in an unexpected format and unable to be parsed. The raw response is: {UNEXPECTED_RESPONSE_FORMAT}", # noqa: E501
),
(
ANOTHER_UNEXPECTED_RESP_FORMAT,
Expand All @@ -185,6 +201,30 @@ def test_formatted_response_raises_errors(
BadResponseFormat,
f"The response was in an unexpected format and unable to be parsed. The raw response is: {ANOTHER_UNEXPECTED_RESP_FORMAT}", # noqa: E501
),
(
INVALID_CODE_RESP_FORMAT,
(),
identity,
identity,
BadResponseFormat,
re.escape(
f"The response was in an unexpected format and unable to be parsed. "
f"The error is: error['code'] must be an integer. "
f"The raw response is: {INVALID_CODE_RESP_FORMAT}"
), # noqa: E501
),
(
INVALID_MESSAGE_RESP_FORMAT,
(),
identity,
identity,
BadResponseFormat,
re.escape(
f"The response was in an unexpected format and unable to be parsed. "
f"The error is: error['message'] must be a string, integer or null. "
f"The raw response is: {INVALID_MESSAGE_RESP_FORMAT}"
), # noqa: E501
),
],
)
def test_formatted_response_raises_correct_error_message(
Expand Down
24 changes: 13 additions & 11 deletions web3/_utils/contract_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,22 @@ def raise_contract_logic_error_on_revert(response: RPCResponse) -> RPCResponse:
message_present = message is not None and message != ""
data = error.get("data", MISSING_DATA)

if message_present:
if data is None:
raise ContractLogicError(message, data=data)
elif isinstance(data, dict):
raise ContractLogicError(f"execution reverted: {message}", data=data)
elif code == 3:
# Geth Revert with error message and code 3 case:
if data is None:
if message_present:
raise ContractLogicError(message, data=data)
elif "execution reverted" in message:
elif not message_present:
raise ContractLogicError("execution reverted", data=data)

if data is None and not message_present:
raise ContractLogicError("execution reverted", data=data)
elif isinstance(data, dict) and message_present:
raise ContractLogicError(f"execution reverted: {message}", data=data)

_raise_error_from_decoded_revert_data(data)

if message_present:
# Geth Revert with error message and code 3 case:
if code == 3:
raise ContractLogicError(message, data=data)
# Geth Revert without error message case:
elif "execution reverted" in message:
raise ContractLogicError("execution reverted", data=data)

return response
11 changes: 6 additions & 5 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ def formatted_response(
if not isinstance(code, int):
raise BadResponseFormat(
"The response was in an unexpected format and unable to be parsed. "
f'The error is: error["code"] must be an integer. '
f"The error is: error['code'] must be an integer. "
f"The raw response is: {response}"
)
elif code == METHOD_NOT_FOUND:
raise MethodUnavailable(error)

if not isinstance(error.get("message"), (str, int, None)):
if not type(error.get("message")) in (str, int, None):
raise BadResponseFormat(
"The response was in an unexpected format and unable to be parsed. "
f'The error is: error["code"] must be an integer. '
f"The raw response is: {response}"
f"The error is: error['message'] must be a string, integer "
f"or null. The raw response is: {response}"
)

apply_error_formatters(error_formatters, response)
Expand All @@ -264,7 +264,8 @@ def formatted_response(

# Response from eth_subscribe includes response["params"]["result"]
elif (
response.get("params") is None and response["params"].get("result") is None
response.get("params") is not None
and response["params"].get("result") is not None
):
return response["params"]["result"]

Expand Down

0 comments on commit 117b5cd

Please sign in to comment.