Skip to content

Commit 699bf6c

Browse files
committed
Raise new MethodUnavailable error if the JSON-RPC method is not supported by the provider
1 parent d99d7db commit 699bf6c

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

tests/core/manager/test_response_formatters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
BadResponseFormat,
1414
BlockNotFound,
1515
ContractLogicError,
16+
MethodUnavailable,
1617
TransactionNotFound,
1718
)
1819

@@ -32,6 +33,14 @@
3233
"message": "You cannot query logs for more than 10000 blocks at once.",
3334
"method": "eth_getLogs",
3435
}
36+
METHOD_NOT_FOUND_RESP_FORMAT = {
37+
"jsonrpc": "2.0",
38+
"error": {
39+
"code": -32601,
40+
"message": "the method eth_getTransactionByHash does not exist/is not "
41+
"available",
42+
},
43+
}
3544

3645

3746
def raise_contract_logic_error(response):
@@ -105,6 +114,13 @@ def raise_contract_logic_error(response):
105114
raise_transaction_not_found,
106115
TransactionNotFound,
107116
),
117+
(
118+
METHOD_NOT_FOUND_RESP_FORMAT,
119+
(),
120+
identity,
121+
identity,
122+
MethodUnavailable,
123+
),
108124
],
109125
)
110126
def test_formatted_response_raises_errors(

web3/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,11 @@ class BadResponseFormat(Web3Exception):
287287
"""
288288

289289
pass
290+
291+
292+
class MethodUnavailable(Web3Exception):
293+
"""
294+
Raised when the method is not available on the node
295+
"""
296+
297+
pass

web3/manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535
from web3.exceptions import (
3636
BadResponseFormat,
37+
MethodUnavailable,
3738
)
3839
from web3.middleware import (
3940
abi_middleware,
@@ -188,6 +189,8 @@ def formatted_response(
188189
) -> Any:
189190
if "error" in response:
190191
apply_error_formatters(error_formatters, response)
192+
if response["error"].get("code") == -32601:
193+
raise MethodUnavailable(response["error"])
191194
raise ValueError(response["error"])
192195
# NULL_RESPONSES includes None, so return False here as the default
193196
# so we don't apply the null_result_formatters if there is no 'result' key

0 commit comments

Comments
 (0)