Skip to content
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

fix: issue where NotImplmeneted error was not raised #1802

Merged
merged 1 commit into from
Jan 2, 2024
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
23 changes: 11 additions & 12 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import sys
import time
from abc import ABC
Expand Down Expand Up @@ -958,6 +959,16 @@ def _make_request(self, endpoint: str, parameters: Optional[List] = None) -> Any
message = (
error["message"] if isinstance(error, dict) and "message" in error else str(error)
)

if (
"does not exist/is not available" in str(message)
or re.match(r"Method .*?not found", message)
or message.startswith("Unknown RPC Endpoint")
):
raise APINotImplementedError(
f"RPC method '{endpoint}' is not implemented by this node instance."
)

raise ProviderError(message)

elif "result" in result:
Expand Down Expand Up @@ -1252,18 +1263,6 @@ def _get_contract_creation_receipt(self, address: AddressType) -> Optional[Recei

return None

def _make_request(self, endpoint: str, parameters: Optional[List] = None) -> Any:
parameters = parameters or []
try:
return super()._make_request(endpoint, parameters)
except ProviderError as err:
if "does not exist/is not available" in str(err):
raise APINotImplementedError(
f"RPC method '{endpoint}' is not implemented by this node instance."
) from err

raise # Original error

def _stream_request(self, method: str, params: List, iter_path="result.item"):
payload = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
results = ijson.sendable_list()
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/geth/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hexbytes import HexBytes

from ape.exceptions import (
APINotImplementedError,
BlockNotFoundError,
NetworkMismatchError,
TransactionError,
Expand Down Expand Up @@ -308,3 +309,11 @@ def test_network_choice_when_custom(geth_provider):
geth_provider.network.name = name

assert actual == "http://127.0.0.1:5550"


def test_make_request_not_exists(geth_provider):
with pytest.raises(
APINotImplementedError,
match="RPC method 'ape_thisDoesNotExist' is not implemented by this node instance.",
):
geth_provider._make_request("ape_thisDoesNotExist")
32 changes: 32 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from web3.exceptions import ContractPanicError

from ape.exceptions import (
APINotImplementedError,
BlockNotFoundError,
ContractLogicError,
ProviderError,
Expand Down Expand Up @@ -342,3 +343,34 @@ def test_network_choice_when_custom(eth_tester_provider):
_ = eth_tester_provider.network_choice
finally:
eth_tester_provider.network.name = name


def test_make_request_not_exists(eth_tester_provider):
with pytest.raises(
APINotImplementedError,
match="RPC method 'ape_thisDoesNotExist' is not implemented by this node instance.",
):
eth_tester_provider._make_request("ape_thisDoesNotExist")


@pytest.mark.parametrize("msg", ("Method not found", "Method ape_thisDoesNotExist not found"))
def test_make_request_not_exists_dev_nodes(eth_tester_provider, mock_web3, msg):
"""
Simulate what *most* of the dev providers do, like hardhat, anvil, and ganache.
"""
real_web3 = eth_tester_provider._web3
mock_web3.eth = real_web3.eth
eth_tester_provider._web3 = mock_web3

def custom_make_request(rpc, params):
if rpc == "ape_thisDoesNotExist":
return {"error": {"message": msg}}

return real_web3.make_request(rpc, params)

mock_web3.provider.make_request.side_effect = custom_make_request
with pytest.raises(
APINotImplementedError,
match="RPC method 'ape_thisDoesNotExist' is not implemented by this node instance.",
):
eth_tester_provider._make_request("ape_thisDoesNotExist")
Loading