Skip to content

Commit c2d716b

Browse files
committed
Add custom exceptions for jsonrpc lookups that return none
1 parent 3db391e commit c2d716b

File tree

5 files changed

+83
-38
lines changed

5 files changed

+83
-38
lines changed

tests/core/eth-module/test_transactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from web3.exceptions import (
44
TimeExhausted,
5+
TransactionNotFound,
56
ValidationError,
67
)
78
from web3.middleware.simulate_unmined_transaction import (
@@ -53,7 +54,7 @@ def test_unmined_transaction_wait_for_receipt(web3):
5354
'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
5455
'value': 123457
5556
})
56-
with pytest.raises(ValueError):
57+
with pytest.raises(TransactionNotFound):
5758
web3.eth.getTransactionReceipt(txn_hash)
5859

5960
txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)

web3/_utils/module_testing/eth_module.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
)
2121

2222
from web3.exceptions import (
23+
BlockNotFound,
2324
InvalidAddress,
25+
TransactionNotFound,
2426
)
2527

2628
UNKNOWN_ADDRESS = '0xdEADBEeF00000000000000000000000000000000'
@@ -278,7 +280,7 @@ def test_eth_replaceTransaction_non_existing_transaction(
278280
'gas': 21000,
279281
'gasPrice': web3.eth.gasPrice,
280282
}
281-
with pytest.raises(ValueError):
283+
with pytest.raises(TransactionNotFound):
282284
web3.eth.replaceTransaction(
283285
'0x98e8cc09b311583c5079fa600f6c2a3bea8611af168c52e4b60b5b243a441997',
284286
txn_params
@@ -485,7 +487,7 @@ def test_eth_getBlockByHash(self, web3, empty_block):
485487
assert block['hash'] == empty_block['hash']
486488

487489
def test_eth_getBlockByHash_not_found(self, web3, empty_block):
488-
with pytest.raises(ValueError):
490+
with pytest.raises(BlockNotFound):
489491
web3.eth.getBlock(UNKNOWN_HASH)
490492

491493
def test_eth_getBlockByNumber_with_integer(self, web3, empty_block):
@@ -498,7 +500,7 @@ def test_eth_getBlockByNumber_latest(self, web3, empty_block):
498500
assert block['number'] == current_block_number
499501

500502
def test_eth_getBlockByNumber_not_found(self, web3, empty_block):
501-
with pytest.raises(ValueError):
503+
with pytest.raises(BlockNotFound):
502504
web3.eth.getBlock(12345)
503505

504506
def test_eth_getBlockByNumber_pending(self, web3, empty_block):
@@ -565,7 +567,7 @@ def test_eth_getTransactionReceipt_unmined(self, web3, unlocked_account_dual_typ
565567
'gas': 21000,
566568
'gasPrice': web3.eth.gasPrice,
567569
})
568-
with pytest.raises(ValueError):
570+
with pytest.raises(TransactionNotFound):
569571
web3.eth.getTransactionReceipt(txn_hash)
570572

571573
def test_eth_getTransactionReceipt_with_log_entry(self,

web3/_utils/transactions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
curry,
99
merge,
1010
)
11+
from web3.exceptions import (
12+
TransactionNotFound,
13+
)
1114

1215
VALID_TRANSACTION_PARAMS = [
1316
'from',
@@ -66,7 +69,7 @@ def wait_for_transaction_receipt(web3, txn_hash, timeout=120, poll_latency=0.1):
6669
while True:
6770
try:
6871
txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
69-
except ValueError:
72+
except TransactionNotFound:
7073
txn_receipt = None
7174
# FIXME: The check for a null `blockHash` is due to parity's
7275
# non-standard implementation of the JSON-RPC API and should

web3/eth.py

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
Contract,
4747
)
4848
from web3.exceptions import (
49+
BlockNotFound,
4950
TimeExhausted,
51+
TransactionNotFound,
5052
)
5153
from web3.iban import (
5254
Iban,
@@ -142,10 +144,13 @@ def getBlock(self, block_identifier, full_transactions=False):
142144
if_number='eth_getBlockByNumber',
143145
)
144146

145-
return self.web3.manager.request_blocking(
146-
method,
147-
[block_identifier, full_transactions],
148-
)
147+
try:
148+
return self.web3.manager.request_blocking(
149+
method,
150+
[block_identifier, full_transactions],
151+
)
152+
except ValueError:
153+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
149154

150155
def getBlockTransactionCount(self, block_identifier):
151156
"""
@@ -158,10 +163,13 @@ def getBlockTransactionCount(self, block_identifier):
158163
if_hash='eth_getBlockTransactionCountByHash',
159164
if_number='eth_getBlockTransactionCountByNumber',
160165
)
161-
return self.web3.manager.request_blocking(
162-
method,
163-
[block_identifier],
164-
)
166+
try:
167+
return self.web3.manager.request_blocking(
168+
method,
169+
[block_identifier],
170+
)
171+
except ValueError:
172+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
165173

166174
def getUncleCount(self, block_identifier):
167175
"""
@@ -174,10 +182,13 @@ def getUncleCount(self, block_identifier):
174182
if_hash='eth_getUncleCountByBlockHash',
175183
if_number='eth_getUncleCountByBlockNumber',
176184
)
177-
return self.web3.manager.request_blocking(
178-
method,
179-
[block_identifier],
180-
)
185+
try:
186+
return self.web3.manager.request_blocking(
187+
method,
188+
[block_identifier],
189+
)
190+
except ValueError:
191+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
181192

182193
def getUncleByBlock(self, block_identifier, uncle_index):
183194
"""
@@ -190,16 +201,24 @@ def getUncleByBlock(self, block_identifier, uncle_index):
190201
if_hash='eth_getUncleByBlockHashAndIndex',
191202
if_number='eth_getUncleByBlockNumberAndIndex',
192203
)
193-
return self.web3.manager.request_blocking(
194-
method,
195-
[block_identifier, uncle_index],
196-
)
204+
try:
205+
return self.web3.manager.request_blocking(
206+
method,
207+
[block_identifier, uncle_index],
208+
)
209+
except ValueError:
210+
raise BlockNotFound(
211+
f"Uncle at index: {uncle_index} of block with id: {block_identifier} not found."
212+
)
197213

198214
def getTransaction(self, transaction_hash):
199-
return self.web3.manager.request_blocking(
200-
"eth_getTransactionByHash",
201-
[transaction_hash],
202-
)
215+
try:
216+
return self.web3.manager.request_blocking(
217+
"eth_getTransactionByHash",
218+
[transaction_hash],
219+
)
220+
except ValueError:
221+
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
203222

204223
@deprecated_for("w3.eth.getTransactionByBlock")
205224
def getTransactionFromBlock(self, block_identifier, transaction_index):
@@ -220,10 +239,16 @@ def getTransactionByBlock(self, block_identifier, transaction_index):
220239
if_hash='eth_getTransactionByBlockHashAndIndex',
221240
if_number='eth_getTransactionByBlockNumberAndIndex',
222241
)
223-
return self.web3.manager.request_blocking(
224-
method,
225-
[block_identifier, transaction_index],
226-
)
242+
try:
243+
return self.web3.manager.request_blocking(
244+
method,
245+
[block_identifier, transaction_index],
246+
)
247+
except ValueError:
248+
raise TransactionNotFound(
249+
f"Transaction index: {transaction_index} "
250+
f"on block id: {block_identifier} not found."
251+
)
227252

228253
def waitForTransactionReceipt(self, transaction_hash, timeout=120):
229254
try:
@@ -237,20 +262,20 @@ def waitForTransactionReceipt(self, transaction_hash, timeout=120):
237262
)
238263

239264
def getTransactionReceipt(self, transaction_hash):
240-
return self.web3.manager.request_blocking(
241-
"eth_getTransactionReceipt",
242-
[transaction_hash],
243-
)
265+
try:
266+
return self.web3.manager.request_blocking(
267+
"eth_getTransactionReceipt",
268+
[transaction_hash],
269+
)
270+
except ValueError:
271+
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
244272

245273
def getTransactionCount(self, account, block_identifier=None):
246274
if block_identifier is None:
247275
block_identifier = self.defaultBlock
248276
return self.web3.manager.request_blocking(
249277
"eth_getTransactionCount",
250-
[
251-
account,
252-
block_identifier,
253-
],
278+
[account, block_identifier],
254279
)
255280

256281
def replaceTransaction(self, transaction_hash, new_transaction):

web3/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@ class ManifestValidationError(PMError):
128128
Raised when a provided manifest cannot be published, since it's invalid.
129129
"""
130130
pass
131+
132+
133+
class TransactionNotFound(Exception):
134+
"""
135+
Raised when a tx hash used to lookup a tx in a jsonrpc call cannot be found.
136+
"""
137+
pass
138+
139+
140+
class BlockNotFound(Exception):
141+
"""
142+
Raised when a block id used to lookup a block in a jsonrpc call cannot be found.
143+
"""
144+
pass

0 commit comments

Comments
 (0)