Skip to content

Commit

Permalink
feat: improve blocknotfound (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 12, 2023
1 parent faeaeb7 commit e4b7ef4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def get_block(self, block_id: BlockID) -> BlockAPI:
try:
block_data = dict(self.web3.eth.get_block(block_id))
except Exception as err:
raise BlockNotFoundError(block_id) from err
raise BlockNotFoundError(block_id, reason=str(err)) from err

# Some nodes (like anvil) will not have a base fee if set to 0.
if "baseFeePerGas" in block_data and block_data.get("baseFeePerGas") is None:
Expand Down
12 changes: 10 additions & 2 deletions src/ape/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,21 @@ class BlockNotFoundError(ProviderError):
Raised when unable to find a block.
"""

def __init__(self, block_id: "BlockID"):
def __init__(self, block_id: "BlockID", reason: Optional[str] = None):
if isinstance(block_id, bytes):
block_id_str = block_id.hex()
else:
block_id_str = str(block_id)

super().__init__(f"Block with ID '{block_id_str}' not found.")
message = (
"Missing latest block."
if block_id == "latest"
else f"Block with ID '{block_id_str}' not found."
)
if reason:
message = f"{message} Reason: {reason}"

super().__init__(message)


class TransactionNotFoundError(ProviderError):
Expand Down

0 comments on commit e4b7ef4

Please sign in to comment.