Skip to content

Commit

Permalink
fix: show network in error (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 3, 2023
1 parent ea67d49 commit 26e9247
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
18 changes: 7 additions & 11 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,7 @@ def get_provider_id(cls, provider: "ProviderAPI") -> Optional[str]:
if not provider.is_connected:
return None

return (
f"{provider.network.ecosystem.name}:"
f"{provider.network.name}:{provider.name}-"
f"{provider.chain_id}"
)
return f"{provider.network_choice}:-{provider.chain_id}"


class NetworkAPI(BaseInterfaceModel):
Expand Down Expand Up @@ -694,8 +690,7 @@ def __repr__(self) -> str:
# Only happens on local networks
chain_id = None

network_key = f"{self.ecosystem.name}:{self.name}"
content = f"{network_key} chain_id={self.chain_id}" if chain_id is not None else network_key
content = f"{self.choice} chain_id={self.chain_id}" if chain_id is not None else self.choice
return f"<{content}>"

@property
Expand Down Expand Up @@ -925,6 +920,10 @@ def default_provider(self) -> Optional[str]:

return None

@property
def choice(self) -> str:
return f"{self.ecosystem.name}:{self.name}"

def set_default_provider(self, provider_name: str):
"""
Change the default provider.
Expand All @@ -939,10 +938,7 @@ def set_default_provider(self, provider_name: str):
if provider_name in self.providers:
self._default_provider = provider_name
else:
raise NetworkError(
f"Provider '{provider_name}' not found in network "
f"'{self.ecosystem.name}:{self.name}'."
)
raise NetworkError(f"Provider '{provider_name}' not found in network '{self.choice}'.")

def use_default_provider(
self, provider_settings: Optional[Dict] = None
Expand Down
10 changes: 8 additions & 2 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ def get_code(self, address: AddressType, **kwargs) -> ContractCode:
:class:`~ape.types.ContractCode`: The contract bytecode.
"""

@property
def network_choice(self) -> str:
"""
The connected network choice string.
"""
return f"{self.network.choice}:{self.name}"

@raises_not_implemented
def get_storage_at(self, address: AddressType, slot: int) -> bytes: # type: ignore[empty-body]
"""
Expand Down Expand Up @@ -722,8 +729,7 @@ def base_fee(self) -> int:
# Use the less-accurate approach (OK for testing).
logger.debug(
"Failed using `web3.eth.fee_history` for network "
f"'{self.network.ecosystem.name}:{self.network.name}:{self.name}'. "
f"Error: {exc}"
f"'{self.network_choice}'. Error: {exc}"
)
return self._get_last_base_fee()

Expand Down
4 changes: 2 additions & 2 deletions src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ def __getitem__(self, address: AddressType) -> ContractType:
if not contract_type:
# Create error message from custom exception cls.
err = ContractNotFoundError(
address, self.provider.network.explorer is not None, self.provider.name
address, self.provider.network.explorer is not None, self.provider.network_choice
)
# Must raise IndexError.
raise IndexError(str(err))
Expand Down Expand Up @@ -1185,7 +1185,7 @@ def instance_at(
raise ContractNotFoundError(
contract_address,
self.provider.network.explorer is not None,
self.provider.name,
self.provider.network_choice,
)

elif not isinstance(contract_type, ContractType):
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_instance_at_contract_type_not_found(chain):
new_address = "0x4a986a6dca6dbF99Bc3D17F8d71aFB0D60E740F9"
expected = (
rf"Failed to get contract type for address '{new_address}'. "
r"Current provider 'test' has no associated explorer plugin. "
r"Current provider 'ethereum:local:test' has no associated explorer plugin. "
"Try installing an explorer plugin using .*ape plugins install etherscan.*, "
r"or using a network with explorer support\."
)
Expand All @@ -409,7 +409,7 @@ def test_contracts_getitem_contract_not_found(chain):
new_address = "0x4a986a6dca6dbF99Bc3D17F8d71aFB0D60E740F9"
expected = (
rf"Failed to get contract type for address '{new_address}'. "
r"Current provider 'test' has no associated explorer plugin. "
r"Current provider 'ethereum:local:test' has no associated explorer plugin. "
"Try installing an explorer plugin using .*ape plugins install etherscan.*, "
r"or using a network with explorer support\."
)
Expand Down

0 comments on commit 26e9247

Please sign in to comment.