Skip to content

Commit 0777571

Browse files
danielAngel98518
authored andcommitted
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 612648e commit 0777571

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
@@ -850,7 +850,7 @@ def normalize_hyperparameters(
850850
norm_value = norm_value.to_dict()
851851
else:
852852
norm_value = value
853-
except Exception:
853+
except (KeyError, ValueError, TypeError, AttributeError):
854854
# bittensor.logging.warning(f"Error normalizing parameter '{param}': {e}")
855855
norm_value = "-"
856856
if not json_output:
@@ -1728,7 +1728,7 @@ def is_valid_github_url(url: str) -> bool:
17281728
return False
17291729

17301730
return True
1731-
except Exception: # TODO figure out the exceptions that can be raised in here
1731+
except (ValueError, TypeError, AttributeError):
17321732
return False
17331733

17341734

bittensor_cli/src/commands/wallets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async def new_hotkey(
377377
if uri:
378378
try:
379379
keypair = Keypair.create_from_uri(uri)
380-
except Exception as e:
380+
except (ValueError, TypeError) as e:
381381
print_error(f"Failed to create keypair from URI {uri}: {str(e)}")
382382
return
383383
wallet.set_hotkey(keypair=keypair, encrypt=use_password)
@@ -428,7 +428,7 @@ async def new_coldkey(
428428
if uri:
429429
try:
430430
keypair = Keypair.create_from_uri(uri)
431-
except Exception as e:
431+
except (ValueError, TypeError) as e:
432432
print_error(f"Failed to create keypair from URI {uri}: {str(e)}")
433433
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False)
434434
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False)
@@ -501,7 +501,7 @@ async def wallet_create(
501501
"hotkey_ss58": wallet.hotkeypub.ss58_address,
502502
"coldkey_ss58": wallet.coldkeypub.ss58_address,
503503
}
504-
except Exception as e:
504+
except (ValueError, TypeError, KeyFileError) as e:
505505
err = f"Failed to create keypair from URI: {str(e)}"
506506
print_error(err)
507507
output_dict["error"] = err

0 commit comments

Comments
 (0)