Skip to content

Commit 8d7de2c

Browse files
authored
Merge pull request #193 from opentensor/fix/thewhaleking/clean-up-error-handling
Clean Up Error Handling
2 parents a5415d8 + 9c427d5 commit 8d7de2c

File tree

2 files changed

+33
-73
lines changed

2 files changed

+33
-73
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,8 @@ async def retrieve_pending_extrinsics(self) -> list:
14071407
runtime = await self.init_runtime()
14081408

14091409
result_data = await self.rpc_request("author_pendingExtrinsics", [])
1410-
1410+
if "error" in result_data:
1411+
raise SubstrateRequestException(result_data["error"]["message"])
14111412
extrinsics = []
14121413

14131414
for extrinsic_data in result_data["result"]:
@@ -2141,6 +2142,8 @@ async def get_parent_block_hash(self, block_hash) -> str:
21412142

21422143
async def _get_parent_block_hash(self, block_hash) -> str:
21432144
block_header = await self.rpc_request("chain_getHeader", [block_hash])
2145+
if "error" in block_header:
2146+
raise SubstrateRequestException(block_header["error"]["message"])
21442147

21452148
if block_header["result"] is None:
21462149
raise SubstrateRequestException(f'Block not found for "{block_hash}"')
@@ -2172,15 +2175,7 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
21722175
response = await self.rpc_request(
21732176
"state_getStorage", [storage_key, block_hash]
21742177
)
2175-
2176-
if "result" in response:
2177-
return response.get("result")
2178-
elif "error" in response:
2179-
raise SubstrateRequestException(response["error"]["message"])
2180-
else:
2181-
raise SubstrateRequestException(
2182-
"Unknown error occurred during retrieval of events"
2183-
)
2178+
return response.get("result")
21842179

21852180
@cached_fetcher(max_size=SUBSTRATE_RUNTIME_CACHE_SIZE)
21862181
async def get_block_runtime_info(self, block_hash: str) -> dict:
@@ -2236,9 +2231,6 @@ async def get_block_metadata(
22362231
params = [block_hash]
22372232
response = await self.rpc_request("state_getMetadata", params)
22382233

2239-
if "error" in response:
2240-
raise SubstrateRequestException(response["error"]["message"])
2241-
22422234
if (result := response.get("result")) and decode:
22432235
metadata_decoder = runtime_config.create_scale_object(
22442236
"MetadataVersioned", data=ScaleBytes(result)
@@ -2553,7 +2545,7 @@ async def _get_block_hash(self, block_id: int) -> str:
25532545
return (await self.rpc_request("chain_getBlockHash", [block_id]))["result"]
25542546

25552547
async def get_chain_head(self) -> str:
2556-
result = await self._make_rpc_request(
2548+
response = await self._make_rpc_request(
25572549
[
25582550
self.make_payload(
25592551
"rpc_request",
@@ -2562,8 +2554,11 @@ async def get_chain_head(self) -> str:
25622554
)
25632555
]
25642556
)
2565-
self.last_block_hash = result["rpc_request"][0]["result"]
2566-
return result["rpc_request"][0]["result"]
2557+
result = response["rpc_request"][0]
2558+
if "error" in result:
2559+
raise SubstrateRequestException(result["error"]["message"])
2560+
self.last_block_hash = result["result"]
2561+
return result["result"]
25672562

25682563
async def compose_call(
25692564
self,
@@ -2690,9 +2685,6 @@ async def query_multi(
26902685
runtime=runtime,
26912686
)
26922687

2693-
if "error" in response:
2694-
raise SubstrateRequestException(response["error"]["message"])
2695-
26962688
result = []
26972689

26982690
storage_key_map = {s.to_hex(): s for s in storage_keys}
@@ -3044,12 +3036,7 @@ async def get_chain_finalised_head(self):
30443036
30453037
"""
30463038
response = await self.rpc_request("chain_getFinalizedHead", [])
3047-
3048-
if response is not None:
3049-
if "error" in response:
3050-
raise SubstrateRequestException(response["error"]["message"])
3051-
3052-
return response.get("result")
3039+
return response["result"]
30533040

30543041
async def _do_runtime_call_old(
30553042
self,
@@ -3092,6 +3079,8 @@ async def _do_runtime_call_old(
30923079
[f"{api}_{method}", param_data.hex(), block_hash],
30933080
runtime=runtime,
30943081
)
3082+
if "error" in result_data:
3083+
raise SubstrateRequestException(result_data["error"]["message"])
30953084
result_vec_u8_bytes = hex_to_bytes(result_data["result"])
30963085
result_bytes = await self.decode_scale(
30973086
"Vec<u8>", result_vec_u8_bytes, runtime=runtime
@@ -3185,6 +3174,8 @@ async def runtime_call(
31853174
[f"{api}_{method}", param_data.hex(), block_hash],
31863175
runtime=runtime,
31873176
)
3177+
if "error" in result_data:
3178+
raise SubstrateRequestException(result_data["error"]["message"])
31883179
output_type_string = f"scale_info::{runtime_call_def['output']}"
31893180

31903181
# Decode result
@@ -3237,6 +3228,8 @@ async def get_account_next_index(self, account_address: str) -> int:
32373228
nonce_obj = await self.rpc_request(
32383229
"account_nextIndex", [account_address]
32393230
)
3231+
if "error" in nonce_obj:
3232+
raise SubstrateRequestException(nonce_obj["error"]["message"])
32403233
self._nonces[account_address] = nonce_obj["result"]
32413234
else:
32423235
self._nonces[account_address] += 1
@@ -3622,9 +3615,6 @@ async def query_map(
36223615
method="state_getKeys", params=[prefix, block_hash], runtime=runtime
36233616
)
36243617

3625-
if "error" in response:
3626-
raise SubstrateRequestException(response["error"]["message"])
3627-
36283618
result_keys = response.get("result")
36293619

36303620
result = []
@@ -3640,8 +3630,6 @@ async def query_map(
36403630
params=[result_keys, block_hash],
36413631
runtime=runtime,
36423632
)
3643-
if "error" in response:
3644-
raise SubstrateRequestException(response["error"]["message"])
36453633
for result_group in response["result"]:
36463634
result = decode_query_map(
36473635
result_group["changes"],
@@ -3680,8 +3668,6 @@ async def query_map(
36803668
)
36813669
)
36823670
for response in all_responses:
3683-
if "error" in response:
3684-
raise SubstrateRequestException(response["error"]["message"])
36853671
for result_group in response["result"]:
36863672
changes.extend(result_group["changes"])
36873673

@@ -3905,9 +3891,6 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39053891
"author_submitExtrinsic", [str(extrinsic.data)]
39063892
)
39073893

3908-
if "result" not in response:
3909-
raise SubstrateRequestException(response.get("error"))
3910-
39113894
result = AsyncExtrinsicReceipt(
39123895
substrate=self, extrinsic_hash=response["result"]
39133896
)
@@ -3994,12 +3977,8 @@ async def get_block_number(self, block_hash: Optional[str] = None) -> int:
39943977
"""Async version of `substrateinterface.base.get_block_number` method."""
39953978
response = await self.rpc_request("chain_getHeader", [block_hash])
39963979

3997-
if "error" in response:
3998-
raise SubstrateRequestException(response["error"]["message"])
3999-
4000-
elif "result" in response:
4001-
if response["result"]:
4002-
return int(response["result"]["number"], 16)
3980+
if response["result"]:
3981+
return int(response["result"]["number"], 16)
40033982
raise SubstrateRequestException(
40043983
f"Unable to retrieve block number for {block_hash}"
40053984
)

async_substrate_interface/sync_substrate.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,6 @@ def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
17091709

17101710
if "result" in response:
17111711
return response.get("result")
1712-
elif "error" in response:
1713-
raise SubstrateRequestException(response["error"]["message"])
17141712
else:
17151713
raise SubstrateRequestException(
17161714
"Unknown error occurred during retrieval of events"
@@ -1761,9 +1759,6 @@ def get_block_metadata(
17611759
params = [block_hash]
17621760
response = self.rpc_request("state_getMetadata", params)
17631761

1764-
if "error" in response:
1765-
raise SubstrateRequestException(response["error"]["message"])
1766-
17671762
if (result := response.get("result")) and decode:
17681763
metadata_decoder = self.runtime_config.create_scale_object(
17691764
"MetadataVersioned", data=ScaleBytes(result)
@@ -2073,7 +2068,7 @@ def get_block_hash(self, block_id: int) -> str:
20732068
return self.rpc_request("chain_getBlockHash", [block_id])["result"]
20742069

20752070
def get_chain_head(self) -> str:
2076-
result = self._make_rpc_request(
2071+
response = self._make_rpc_request(
20772072
[
20782073
self.make_payload(
20792074
"rpc_request",
@@ -2082,8 +2077,11 @@ def get_chain_head(self) -> str:
20822077
)
20832078
]
20842079
)
2085-
self.last_block_hash = result["rpc_request"][0]["result"]
2086-
return result["rpc_request"][0]["result"]
2080+
result = response["rpc_request"][0]
2081+
if "error" in result:
2082+
raise SubstrateRequestException(result["error"]["message"])
2083+
self.last_block_hash = result["result"]
2084+
return result["result"]
20872085

20882086
def compose_call(
20892087
self,
@@ -2191,9 +2189,6 @@ def query_multi(
21912189
"state_queryStorageAt", [[s.to_hex() for s in storage_keys], block_hash]
21922190
)
21932191

2194-
if "error" in response:
2195-
raise SubstrateRequestException(response["error"]["message"])
2196-
21972192
result = []
21982193

21992194
storage_key_map = {s.to_hex(): s for s in storage_keys}
@@ -2528,12 +2523,7 @@ def get_chain_finalised_head(self):
25282523
25292524
"""
25302525
response = self.rpc_request("chain_getFinalizedHead", [])
2531-
2532-
if response is not None:
2533-
if "error" in response:
2534-
raise SubstrateRequestException(response["error"]["message"])
2535-
2536-
return response.get("result")
2526+
return response["result"]
25372527

25382528
def _do_runtime_call_old(
25392529
self,
@@ -3051,9 +3041,6 @@ def query_map(
30513041
params=[prefix, page_size, start_key, block_hash],
30523042
)
30533043

3054-
if "error" in response:
3055-
raise SubstrateRequestException(response["error"]["message"])
3056-
30573044
result_keys = response.get("result")
30583045

30593046
result = []
@@ -3067,9 +3054,6 @@ def query_map(
30673054
method="state_queryStorageAt", params=[result_keys, block_hash]
30683055
)
30693056

3070-
if "error" in response:
3071-
raise SubstrateRequestException(response["error"]["message"])
3072-
30733057
for result_group in response["result"]:
30743058
result = decode_query_map(
30753059
result_group["changes"],
@@ -3376,15 +3360,12 @@ def get_block_number(self, block_hash: Optional[str] = None) -> int:
33763360
"""Async version of `substrateinterface.base.get_block_number` method."""
33773361
response = self.rpc_request("chain_getHeader", [block_hash])
33783362

3379-
if "error" in response:
3380-
raise SubstrateRequestException(response["error"]["message"])
3381-
3382-
elif "result" in response:
3383-
if response["result"]:
3384-
return int(response["result"]["number"], 16)
3385-
raise SubstrateRequestException(
3386-
f"Unable to determine block number for {block_hash}"
3387-
)
3363+
if response["result"]:
3364+
return int(response["result"]["number"], 16)
3365+
else:
3366+
raise SubstrateRequestException(
3367+
f"Unable to determine block number for {block_hash}"
3368+
)
33883369

33893370
def close(self):
33903371
"""

0 commit comments

Comments
 (0)