Skip to content

Commit

Permalink
fix: use helpful IndexError in AccountManager.__getitem__() when …
Browse files Browse the repository at this point in the history
…conversion error occurs (#1798)

Co-authored-by: El De-dog-lo <[email protected]>
  • Loading branch information
antazoey and fubuloubu authored Dec 22, 2023
1 parent d80dcc5 commit b8dba14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/ape/managers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
TestAccountAPI,
TestAccountContainerAPI,
)
from ape.exceptions import ConversionError
from ape.managers.base import BaseManager
from ape.types import AddressType
from ape.utils import ManagerAccessMixin, cached_property, singledispatchmethod

from .base import BaseManager

_DEFAULT_SENDERS: List[AccountAPI] = []


Expand Down Expand Up @@ -93,14 +93,19 @@ def __getitem_slice(self, account_id: slice):

@__getitem__.register
def __getitem_str(self, account_str: str):
account_id = self.conversion_manager.convert(account_str, AddressType)
message_fmt = "No account with {} '{}'."
try:
account_id = self.conversion_manager.convert(account_str, AddressType)
except ConversionError as err:
message = message_fmt.format("ID", account_str)
raise IndexError(message) from err

for account in self.accounts:
if account.address == account_id:
return account

can_impersonate = False
err_message = f"No account with address '{account_id}'."
err_message = message_fmt.format("address", account_id)
try:
if self.network_manager.active_provider:
can_impersonate = self.provider.unlock_account(account_id)
Expand Down Expand Up @@ -329,7 +334,16 @@ def __getitem_str(self, account_str: str) -> AccountAPI:
:class:`~ape.api.accounts.AccountAPI`
"""

account_id = self.conversion_manager.convert(account_str, AddressType)
try:
account_id = self.conversion_manager.convert(account_str, AddressType)
except ConversionError as err:
prefix = f"No account with ID '{account_str}'"
if account_str.endswith(".eth"):
suffix = "Do you have `ape-ens` installed?"
else:
suffix = "Do you have the necessary conversion plugins installed?"

raise IndexError(f"{prefix}. {suffix}") from err

for container in self.containers.values():
if account_id in container.accounts:
Expand Down
28 changes: 28 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ def test_accounts_address_access(owner, accounts):
assert accounts[owner.address] == owner


def test_accounts_address_access_conversion_fail(accounts):
with pytest.raises(
IndexError,
match=(
r"No account with ID 'FAILS'\. "
r"Do you have the necessary conversion plugins installed?"
),
):
_ = accounts["FAILS"]


def test_accounts_address_access_not_found(accounts):
address = "0x1222262222222922222222222222222222222222"
with pytest.raises(IndexError, match=rf"No account with address '{address}'\."):
_ = accounts[address]


def test_test_accounts_address_access_conversion_fail(test_accounts):
with pytest.raises(IndexError, match=r"No account with ID 'FAILS'"):
_ = test_accounts["FAILS"]


def test_test_accounts_address_access_not_found(test_accounts):
address = "0x1222262222222922222222222222222222222222"
with pytest.raises(IndexError, match=rf"No account with address '{address}'\."):
_ = test_accounts[address]


def test_accounts_contains(accounts, owner):
assert owner.address in accounts

Expand Down

0 comments on commit b8dba14

Please sign in to comment.