Skip to content

Commit

Permalink
feat: NetworkNotFoundError handle case when no networks (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 7, 2024
1 parent 6286f48 commit 091f600
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/ape/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,19 @@ def __init__(
options: Optional[Collection[str]] = None,
):
self.network = network
message = (
f"No network in '{ecosystem}' named '{network}'."
if ecosystem
else f"No network named '{network}'."
)
options = options or []
if network in options:
# Only seen in testing scenarios. Not realistic.
raise ValueError(
f"{network} found in options. Should not have gotten `NetworkNotFoundError`."
)

if options:
message = (
f"No network in '{ecosystem}' named '{network}'."
if ecosystem
else f"No network named '{network}'."
)
close_matches = difflib.get_close_matches(network, options, cutoff=0.6)
if close_matches:
message = f"{message} Did you mean '{', '.join(close_matches)}'?"
Expand All @@ -371,6 +378,12 @@ def __init__(
options_str = "\n".join(sorted(options))
message = f"{message} Options:\n{options_str}"

elif ecosystem:
message = f"'{ecosystem}' has no networks."

else:
message = "No networks found."

super().__init__(message)


Expand Down
34 changes: 33 additions & 1 deletion tests/functional/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from ape.api import ReceiptAPI
from ape.exceptions import Abort, TransactionError
from ape.exceptions import Abort, NetworkNotFoundError, TransactionError
from ape_ethereum.transactions import Receipt


Expand All @@ -25,3 +25,35 @@ class SubclassReceipt(Receipt):

err = TransactionError(txn=sub_receipt)
assert isinstance(err.txn, ReceiptAPI) # Same check used.


def test_network_not_found_error_close_match():
net = "sepolai"
error = NetworkNotFoundError(net, ecosystem="ethereum", options=("sepolia",))
actual = str(error)
expected = f"No network in 'ethereum' named '{net}'. Did you mean 'sepolia'?"
assert actual == expected


def test_network_not_found_error_no_close_matches():
net = "madeup"
error = NetworkNotFoundError(net, ecosystem="ethereum", options=("sepolia",))
actual = str(error)
expected = f"No network in 'ethereum' named '{net}'. Options:\nsepolia"
assert actual == expected


def test_network_with_ecosystem_not_found_no_options():
net = "madeup"
error = NetworkNotFoundError(net, ecosystem="ethereum", options=())
actual = str(error)
expected = "'ethereum' has no networks."
assert actual == expected


def test_network_without_ecosystem_not_found_no_options():
net = "madeup"
error = NetworkNotFoundError(net, options=())
actual = str(error)
expected = "No networks found."
assert actual == expected

0 comments on commit 091f600

Please sign in to comment.