Skip to content

Commit 4a5b46f

Browse files
author
daniel
committed
fix: replace broad exception catches with specific exception types
Replace overly broad `except Exception` catches with specific exception types to improve error handling clarity and debugging: - subtensor_interface.py: Handle both TimeoutError and asyncio.TimeoutError for substrate initialization timeout (resolves TODO comment) - utils.py (is_valid_github_url): Catch ValueError, TypeError, AttributeError for URL parsing exceptions (resolves TODO comment) - utils.py (normalize_hyperparameters): Catch KeyError, ValueError, TypeError, AttributeError for parameter normalization - wallets.py (new_hotkey): Catch ValueError, TypeError for Keypair.create_from_uri - wallets.py (new_coldkey): Catch ValueError, TypeError for Keypair.create_from_uri - wallets.py (wallet_create): Catch ValueError, TypeError, KeyFileError for keypair and wallet creation This change improves code quality by making exception handling more explicit and easier to debug while maintaining the same error recovery behavior.
1 parent 6209a52 commit 4a5b46f

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

bittensor_cli/src/bittensor/subtensor_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async def __aenter__(self):
135135
try:
136136
await self.substrate.initialize()
137137
return self
138-
except TimeoutError: # TODO verify
138+
except (TimeoutError, asyncio.TimeoutError):
139139
err_console.print(
140140
"\n[red]Error[/red]: Timeout occurred connecting to substrate. "
141141
f"Verify your chain and network settings: {self}"

bittensor_cli/src/bittensor/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ def normalize_hyperparameters(
808808
norm_value = norm_value.to_dict()
809809
else:
810810
norm_value = value
811-
except Exception:
811+
except (KeyError, ValueError, TypeError, AttributeError):
812812
# bittensor.logging.warning(f"Error normalizing parameter '{param}': {e}")
813813
norm_value = "-"
814814
if not json_output:
@@ -1686,7 +1686,7 @@ def is_valid_github_url(url: str) -> bool:
16861686
return False
16871687

16881688
return True
1689-
except Exception: # TODO figure out the exceptions that can be raised in here
1689+
except (ValueError, TypeError, AttributeError):
16901690
return False
16911691

16921692

bittensor_cli/src/commands/wallets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ async def new_hotkey(
374374
if uri:
375375
try:
376376
keypair = Keypair.create_from_uri(uri)
377-
except Exception as e:
377+
except (ValueError, TypeError) as e:
378378
print_error(f"Failed to create keypair from URI {uri}: {str(e)}")
379379
return
380380
wallet.set_hotkey(keypair=keypair, encrypt=use_password)
@@ -425,7 +425,7 @@ async def new_coldkey(
425425
if uri:
426426
try:
427427
keypair = Keypair.create_from_uri(uri)
428-
except Exception as e:
428+
except (ValueError, TypeError) as e:
429429
print_error(f"Failed to create keypair from URI {uri}: {str(e)}")
430430
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False)
431431
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False)
@@ -498,7 +498,7 @@ async def wallet_create(
498498
"hotkey_ss58": wallet.hotkeypub.ss58_address,
499499
"coldkey_ss58": wallet.coldkeypub.ss58_address,
500500
}
501-
except Exception as e:
501+
except (ValueError, TypeError, KeyFileError) as e:
502502
err = f"Failed to create keypair from URI: {str(e)}"
503503
print_error(err)
504504
output_dict["error"] = err

0 commit comments

Comments
 (0)