Skip to content

Commit c1ec360

Browse files
committed
Add error formatters to raise on block not found errors
1 parent 9d79211 commit c1ec360

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

web3/_utils/error_formatters_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99

1010
from web3.exceptions import (
11+
BlockNotFound,
1112
ContractCustomError,
1213
ContractLogicError,
1314
ContractPanicError,
@@ -185,3 +186,19 @@ def raise_transaction_indexing_error_if_indexing(response: RPCResponse) -> RPCRe
185186
raise TransactionIndexingInProgress(message)
186187

187188
return response
189+
190+
191+
def raise_block_not_found_on_error(response: RPCResponse) -> RPCResponse:
192+
"""
193+
Raise ``BlockNotFound`` on specific error message(s).
194+
"""
195+
error = response.get("error")
196+
if not isinstance(error, str) and error is not None:
197+
message = error.get("message")
198+
if message is not None:
199+
if "not found" in message.lower() and any(
200+
key in message.lower() for key in ("block", "header")
201+
):
202+
raise BlockNotFound(message)
203+
204+
return response

web3/_utils/method_formatters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
is_length,
5656
)
5757
from web3._utils.error_formatters_utils import (
58+
raise_block_not_found_on_error,
5859
raise_contract_logic_error_on_revert,
5960
raise_transaction_indexing_error_if_indexing,
6061
)
@@ -1092,6 +1093,7 @@ def subscription_formatter(value: Any) -> Union[HexBytes, HexStr, Dict[str, Any]
10921093
RPC.eth_estimateGas: raise_contract_logic_error_on_revert,
10931094
RPC.eth_call: raise_contract_logic_error_on_revert,
10941095
RPC.eth_getTransactionReceipt: raise_transaction_indexing_error_if_indexing,
1096+
RPC.eth_getBlockReceipts: raise_block_not_found_on_error,
10951097
}
10961098

10971099

0 commit comments

Comments
 (0)