Skip to content

Raise new MethodUnavailable error for methods that are unavailable #2796

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

Merged
merged 3 commits into from
Jan 27, 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
3 changes: 3 additions & 0 deletions docs/v6_migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ Other Misc Changes

- ``InfuraKeyNotFound`` exception has been changed to ``InfuraProjectIdNotFound``
- ``SolidityError`` has been removed in favor of ``ContractLogicError``
- When a method is unavailable from a node provider (i.e. a response error
code of -32601 is returned), a ``MethodUnavailable`` error is
now raised instead of ``ValueError``

Removals
~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2796.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When a method is not supported by a node provider, raise a MethodUnavailable error instead of the generic ValueError.
26 changes: 26 additions & 0 deletions tests/core/manager/test_response_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BadResponseFormat,
BlockNotFound,
ContractLogicError,
MethodUnavailable,
TransactionNotFound,
)

Expand All @@ -32,6 +33,17 @@
"message": "You cannot query logs for more than 10000 blocks at once.",
"method": "eth_getLogs",
}
METHOD_NOT_FOUND_RESP_FORMAT = {
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "the method eth_getTransactionByHash does not exist/is not "
"available",
},
}
ETH_TESTER_METHOD_NOT_FOUND_RESP_FORMAT = {
"error": "the method eth_getTransactionByHash does not exist/is not available",
}


def raise_contract_logic_error(response):
Expand Down Expand Up @@ -105,6 +117,20 @@ def raise_contract_logic_error(response):
raise_transaction_not_found,
TransactionNotFound,
),
(
METHOD_NOT_FOUND_RESP_FORMAT,
(),
identity,
identity,
MethodUnavailable,
),
(
ETH_TESTER_METHOD_NOT_FOUND_RESP_FORMAT,
(),
identity,
identity,
ValueError,
),
],
)
def test_formatted_response_raises_errors(
Expand Down
8 changes: 8 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,11 @@ class BadResponseFormat(Web3Exception):
"""

pass


class MethodUnavailable(Web3Exception):
"""
Raised when the method is not available on the node
"""

pass
8 changes: 8 additions & 0 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from web3.exceptions import (
BadResponseFormat,
MethodUnavailable,
)
from web3.middleware import (
abi_middleware,
Expand Down Expand Up @@ -188,6 +189,13 @@ def formatted_response(
) -> Any:
if "error" in response:
apply_error_formatters(error_formatters, response)

# guard against eth-tester case - eth-tester returns a string
# with no code, so can't parse what the error is.
if isinstance(response["error"], dict):
resp_code = response["error"].get("code")
if resp_code == -32601:
raise MethodUnavailable(response["error"])
raise ValueError(response["error"])
# NULL_RESPONSES includes None, so return False here as the default
# so we don't apply the null_result_formatters if there is no 'result' key
Expand Down