Skip to content

Commit 652650d

Browse files
authored
Merge pull request #1268 from njgheorghita/raise-custom-exception
Add custom exceptions for jsonrpc lookups that return none
2 parents eef7c40 + bd899fc commit 652650d

File tree

12 files changed

+74
-48
lines changed

12 files changed

+74
-48
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)

tests/core/middleware/test_latest_block_based_cache_middleware.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ def test_latest_block_based_cache_middleware_busts_cache(w3, mocker):
182182
result = w3.manager.request_blocking('fake_endpoint', [])
183183

184184
assert w3.manager.request_blocking('fake_endpoint', []) == result
185-
with pytest.raises(ValueError):
186-
w3.testing.mine()
185+
w3.testing.mine()
187186

188187
# should still be cached for at least 1 second. This also verifies that
189188
# the middleware caches the latest block based on the block time.
@@ -212,10 +211,8 @@ def result_cb(method, params):
212211
}))
213212
w3.middleware_onion.add(latest_block_based_cache_middleware)
214213

215-
with pytest.raises(ValueError):
216-
w3.manager.request_blocking('fake_endpoint', [])
217-
with pytest.raises(ValueError):
218-
w3.manager.request_blocking('fake_endpoint', [])
214+
w3.manager.request_blocking('fake_endpoint', [])
215+
w3.manager.request_blocking('fake_endpoint', [])
219216

220217
assert next(counter) == 2
221218

tests/core/middleware/test_simple_cache_middleware.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ def result_cb(method, params):
7878
rpc_whitelist={'fake_endpoint'},
7979
))
8080

81-
with pytest.raises(ValueError):
82-
w3.manager.request_blocking('fake_endpoint', [])
83-
with pytest.raises(ValueError):
84-
w3.manager.request_blocking('fake_endpoint', [])
81+
w3.manager.request_blocking('fake_endpoint', [])
82+
w3.manager.request_blocking('fake_endpoint', [])
8583

8684
assert next(counter) == 2
8785

tests/core/middleware/test_time_based_cache_middleware.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ def mk_result(method, params):
117117
w3.middleware_onion.add(construct_result_generator_middleware({'fake_endpoint': mk_result}))
118118
w3.middleware_onion.add(time_cache_middleware)
119119

120-
with pytest.raises(ValueError):
121-
w3.manager.request_blocking('fake_endpoint', [])
122-
with pytest.raises(ValueError):
123-
w3.manager.request_blocking('fake_endpoint', [])
120+
w3.manager.request_blocking('fake_endpoint', [])
121+
w3.manager.request_blocking('fake_endpoint', [])
124122

125123
assert next(counter) == 2
126124

tests/core/testing-module/test_testing_snapshot_and_revert.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import pytest
2-
3-
41
def test_snapshot_revert_to_latest_snapshot(web3):
52
web3.testing.mine(5)
63

@@ -14,8 +11,7 @@ def test_snapshot_revert_to_latest_snapshot(web3):
1411

1512
block_after_mining = web3.eth.getBlock("latest")
1613

17-
with pytest.raises(ValueError):
18-
web3.testing.revert()
14+
web3.testing.revert()
1915

2016
block_after_revert = web3.eth.getBlock("latest")
2117

@@ -42,8 +38,7 @@ def test_snapshot_revert_to_specific(web3):
4238

4339
block_after_mining = web3.eth.getBlock("latest")
4440

45-
with pytest.raises(ValueError):
46-
web3.testing.revert(snapshot_idx)
41+
web3.testing.revert(snapshot_idx)
4742

4843
block_after_revert = web3.eth.getBlock("latest")
4944

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import pytest
2-
3-
41
def test_time_traveling(web3):
52
current_block_time = web3.eth.getBlock("pending")['timestamp']
63

74
time_travel_to = current_block_time + 12345
85

9-
with pytest.raises(ValueError):
10-
web3.testing.timeTravel(time_travel_to)
6+
web3.testing.timeTravel(time_travel_to)
117

128
latest_block_time = web3.eth.getBlock("pending")['timestamp']
139
assert latest_block_time >= time_travel_to

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/module_testing/parity_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
class ParityModuleTest:
1313

1414
def test_list_storage_keys_no_support(self, web3, emitter_contract_address):
15-
with pytest.raises(ValueError):
16-
web3.parity.listStorageKeys(emitter_contract_address, 10, None)
15+
keys = web3.parity.listStorageKeys(emitter_contract_address, 10, None)
16+
assert keys is None
1717

1818
def test_trace_replay_transaction(self, web3, parity_fixture_data):
1919
trace = web3.parity.traceReplayTransaction(parity_fixture_data['mined_txn_hash'])

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: 36 additions & 11 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(
147+
result = self.web3.manager.request_blocking(
146148
method,
147149
[block_identifier, full_transactions],
148150
)
151+
if result is None:
152+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
153+
return result
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(
166+
result = self.web3.manager.request_blocking(
162167
method,
163168
[block_identifier],
164169
)
170+
if result is None:
171+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
172+
return result
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(
185+
result = self.web3.manager.request_blocking(
178186
method,
179187
[block_identifier],
180188
)
189+
if result is None:
190+
raise BlockNotFound(f"Block with id: {block_identifier} not found.")
191+
return result
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(
204+
result = self.web3.manager.request_blocking(
194205
method,
195206
[block_identifier, uncle_index],
196207
)
208+
if result is None:
209+
raise BlockNotFound(
210+
f"Uncle at index: {uncle_index} of block with id: {block_identifier} not found."
211+
)
212+
return result
197213

198214
def getTransaction(self, transaction_hash):
199-
return self.web3.manager.request_blocking(
215+
result = self.web3.manager.request_blocking(
200216
"eth_getTransactionByHash",
201217
[transaction_hash],
202218
)
219+
if result is None:
220+
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
221+
return result
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(
242+
result = self.web3.manager.request_blocking(
224243
method,
225244
[block_identifier, transaction_index],
226245
)
246+
if result is None:
247+
raise TransactionNotFound(
248+
f"Transaction index: {transaction_index} "
249+
f"on block id: {block_identifier} not found."
250+
)
251+
return result
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(
265+
result = self.web3.manager.request_blocking(
241266
"eth_getTransactionReceipt",
242267
[transaction_hash],
243268
)
269+
if result is None:
270+
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
271+
return result
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):

0 commit comments

Comments
 (0)