Skip to content

Commit cb77314

Browse files
authored
Merge pull request #778 from leonace924/fix/error-handling
Fix: update the err_console.print to print_error to make consistency in error printing with cross mark
2 parents 612648e + 5ad7b24 commit cb77314

File tree

23 files changed

+283
-339
lines changed

23 files changed

+283
-339
lines changed

bittensor_cli/cli.py

Lines changed: 50 additions & 52 deletions
Large diffs are not rendered by default.

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from bittensor_cli.src.bittensor.utils import (
3333
confirm_action,
3434
console,
35-
err_console,
35+
print_error,
3636
format_error_message,
3737
millify,
3838
get_human_readable,
@@ -94,7 +94,7 @@ def _get_real_torch():
9494

9595

9696
def log_no_torch_error():
97-
err_console.print(
97+
print_error(
9898
"This command requires torch. You can install torch"
9999
" with `pip install torch` and run the command again."
100100
)
@@ -509,8 +509,8 @@ async def get_neuron_for_pubkey_and_subnet():
509509

510510
print_verbose("Checking subnet status")
511511
if not await subtensor.subnet_exists(netuid):
512-
err_console.print(
513-
f":cross_mark: [red]Failed[/red]: error: [bold white]subnet:{netuid}[/bold white] does not exist."
512+
print_error(
513+
f"Failed: error: [bold white]subnet:{netuid}[/bold white] does not exist."
514514
)
515515
return False
516516

@@ -587,7 +587,7 @@ async def get_neuron_for_pubkey_and_subnet():
587587
subtensor, netuid=netuid, hotkey_ss58=get_hotkey_pub_ss58(wallet)
588588
)
589589
if is_registered:
590-
err_console.print(
590+
print_error(
591591
f":white_heavy_check_mark: [dark_sea_green3]Already registered on netuid:{netuid}[/dark_sea_green3]"
592592
)
593593
return True
@@ -635,9 +635,7 @@ async def get_neuron_for_pubkey_and_subnet():
635635
f"[bold]subnet:{netuid}[/bold][/dark_sea_green3]"
636636
)
637637
return True
638-
err_console.print(
639-
f":cross_mark: [red]Failed[/red]: {err_msg}"
640-
)
638+
print_error(f"Failed: {err_msg}")
641639
await asyncio.sleep(0.5)
642640

643641
# Successful registration, final check for neuron and pubkey
@@ -655,25 +653,23 @@ async def get_neuron_for_pubkey_and_subnet():
655653
return True
656654
else:
657655
# neuron not found, try again
658-
err_console.print(
659-
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
660-
)
656+
print_error("Unknown error. Neuron not found.")
661657
continue
662658
else:
663659
# Exited loop because pow is no longer valid.
664-
err_console.print("[red]POW is stale.[/red]")
660+
print_error("POW is stale.")
665661
# Try again.
666662
continue
667663

668664
if attempts < max_allowed_attempts:
669665
# Failed registration, retry pow
670666
attempts += 1
671-
err_console.print(
667+
print_error(
672668
":satellite: Failed registration, retrying pow ...({attempts}/{max_allowed_attempts})"
673669
)
674670
else:
675671
# Failed to register after max attempts.
676-
err_console.print("[red]No more attempts.[/red]")
672+
print_error("No more attempts.")
677673
return False
678674

679675

@@ -772,7 +768,7 @@ async def burned_register_extrinsic(
772768
)
773769

774770
if not success:
775-
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
771+
print_error(f"Failed: {err_msg}")
776772
await asyncio.sleep(0.5)
777773
return False, err_msg, None
778774
# Successful registration, final check for neuron and pubkey
@@ -808,9 +804,7 @@ async def burned_register_extrinsic(
808804
return True, f"Registered on {netuid} with UID {my_uid}", ext_id
809805
else:
810806
# neuron not found, try again
811-
err_console.print(
812-
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
813-
)
807+
print_error("Unknown error. Neuron not found.")
814808
return False, "Unknown error. Neuron not found.", ext_id
815809

816810

@@ -891,7 +885,7 @@ async def run_faucet_extrinsic(
891885
if cuda:
892886
if not torch.cuda.is_available():
893887
if prompt:
894-
err_console.print("CUDA is not available.")
888+
print_error("CUDA is not available.")
895889
return False, "CUDA is not available."
896890
pow_result = await create_pow(
897891
subtensor,
@@ -936,9 +930,8 @@ async def run_faucet_extrinsic(
936930

937931
# process if registration successful, try again if pow is still valid
938932
if not await response.is_success:
939-
err_console.print(
940-
f":cross_mark: [red]Failed[/red]: "
941-
f"{format_error_message(await response.error_message)}"
933+
print_error(
934+
f"Failed: {format_error_message(await response.error_message)}"
942935
)
943936
if attempts == max_allowed_attempts:
944937
raise MaxAttemptsException
@@ -1788,29 +1781,29 @@ async def swap_hotkey_extrinsic(
17881781
)
17891782

17901783
if netuid is not None and netuid not in netuids_registered:
1791-
err_console.print(
1792-
f":cross_mark: [red]Failed[/red]: Original hotkey {hk_ss58} is not registered on subnet {netuid}"
1784+
print_error(
1785+
f"Failed: Original hotkey {hk_ss58} is not registered on subnet {netuid}"
17931786
)
17941787
return False, None
17951788

17961789
elif not len(netuids_registered) > 0:
1797-
err_console.print(
1790+
print_error(
17981791
f"Original hotkey [dark_orange]{hk_ss58}[/dark_orange] is not registered on any subnet. "
17991792
f"Please register and try again"
18001793
)
18011794
return False, None
18021795

18031796
if netuid is not None:
18041797
if netuid in netuids_registered_new_hotkey:
1805-
err_console.print(
1806-
f":cross_mark: [red]Failed[/red]: New hotkey {new_hk_ss58} "
1798+
print_error(
1799+
f"Failed: New hotkey {new_hk_ss58} "
18071800
f"is already registered on subnet {netuid}"
18081801
)
18091802
return False, None
18101803
else:
18111804
if len(netuids_registered_new_hotkey) > 0:
1812-
err_console.print(
1813-
f":cross_mark: [red]Failed[/red]: New hotkey {new_hk_ss58} "
1805+
print_error(
1806+
f"Failed: New hotkey {new_hk_ss58} "
18141807
f"is already registered on subnet(s) {netuids_registered_new_hotkey}"
18151808
)
18161809
return False, None
@@ -1864,6 +1857,6 @@ async def swap_hotkey_extrinsic(
18641857
)
18651858
return True, ext_receipt
18661859
else:
1867-
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
1860+
print_error(f"Failed: {err_msg}")
18681861
time.sleep(0.5)
18691862
return False, ext_receipt

bittensor_cli/src/bittensor/extrinsics/root.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from bittensor_cli.src.bittensor.utils import (
3232
confirm_action,
3333
console,
34-
err_console,
34+
print_error,
3535
u16_normalized_float,
3636
print_verbose,
3737
format_error_message,
@@ -363,7 +363,7 @@ async def root_register_extrinsic(
363363
)
364364

365365
if not success:
366-
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
366+
print_error(f"Failed: {err_msg}")
367367
await asyncio.sleep(0.5)
368368
return False, err_msg, None
369369

@@ -383,9 +383,7 @@ async def root_register_extrinsic(
383383
return True, f"Registered with UID {uid}", ext_id
384384
else:
385385
# neuron not found, try again
386-
err_console.print(
387-
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
388-
)
386+
print_error("Unknown error. Neuron not found.")
389387
return False, "Unknown error. Neuron not found.", ext_id
390388

391389

@@ -454,7 +452,7 @@ async def _do_set_weights():
454452
)
455453

456454
if my_uid is None:
457-
err_console.print("Your hotkey is not registered to the root network")
455+
print_error("Your hotkey is not registered to the root network")
458456
return False
459457

460458
if not unlock_key(wallet).success:
@@ -546,10 +544,10 @@ async def _do_set_weights():
546544
return True
547545
else:
548546
fmt_err = format_error_message(error_message)
549-
err_console.print(f":cross_mark: [red]Failed[/red]: {fmt_err}")
547+
print_error(f"Failed: {fmt_err}")
550548
return False
551549

552550
except SubstrateRequestException as e:
553551
fmt_err = format_error_message(e)
554-
err_console.print(":cross_mark: [red]Failed[/red]: error:{}".format(fmt_err))
552+
print_error("Failed: error:{}".format(fmt_err))
555553
return False

bittensor_cli/src/bittensor/extrinsics/serving.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from bittensor_cli.src.bittensor.utils import (
1111
confirm_action,
1212
console,
13-
err_console,
13+
print_error,
1414
format_error_message,
1515
unlock_key,
1616
print_extrinsic_id,
@@ -120,7 +120,7 @@ async def reset_axon_extrinsic(
120120
success = await response.is_success
121121
if not success:
122122
error_msg = format_error_message(await response.error_message)
123-
err_console.print(f":cross_mark: [red]Failed[/red]: {error_msg}")
123+
print_error(f"Failed: {error_msg}")
124124
return False, error_msg, None
125125
else:
126126
ext_id = await response.get_extrinsic_identifier()
@@ -132,9 +132,7 @@ async def reset_axon_extrinsic(
132132

133133
except Exception as e:
134134
error_message = format_error_message(e)
135-
err_console.print(
136-
f":cross_mark: [red]Failed to reset axon: {error_message}[/red]"
137-
)
135+
print_error(f"Failed to reset axon: {error_message}")
138136
return False, error_message, None
139137

140138

@@ -240,7 +238,7 @@ async def set_axon_extrinsic(
240238
success = await response.is_success
241239
if not success:
242240
error_msg = format_error_message(await response.error_message)
243-
err_console.print(f":cross_mark: [red]Failed[/red]: {error_msg}")
241+
print_error(f"Failed: {error_msg}")
244242
return False, error_msg, None
245243
else:
246244
ext_id = await response.get_extrinsic_identifier()
@@ -252,7 +250,5 @@ async def set_axon_extrinsic(
252250

253251
except Exception as e:
254252
error_message = format_error_message(e)
255-
err_console.print(
256-
f":cross_mark: [red]Failed to set axon: {error_message}[/red]"
257-
)
253+
print_error(f"Failed to set axon: {error_message}")
258254
return False, error_message, None

bittensor_cli/src/bittensor/extrinsics/transfer.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from bittensor_cli.src.bittensor.utils import (
1212
confirm_action,
1313
console,
14-
err_console,
14+
print_error,
1515
print_verbose,
1616
is_valid_bittensor_address_or_public_key,
1717
print_error,
@@ -95,8 +95,8 @@ async def do_transfer() -> tuple[bool, str, str, Optional[AsyncExtrinsicReceipt]
9595

9696
# Validate destination address.
9797
if not is_valid_bittensor_address_or_public_key(destination):
98-
err_console.print(
99-
f":cross_mark: [red]Invalid destination SS58 address[/red]:[bold white]\n {destination}[/bold white]"
98+
print_error(
99+
f"Invalid destination SS58 address:[bold white]\n {destination}[/bold white]"
100100
)
101101
return False, None
102102
console.print(f"[dark_orange]Initiating transfer on network: {subtensor.network}")
@@ -139,33 +139,33 @@ async def do_transfer() -> tuple[bool, str, str, Optional[AsyncExtrinsicReceipt]
139139

140140
if proxy:
141141
if proxy_balance < (amount + existential_deposit) and not allow_death:
142-
err_console.print(
143-
":cross_mark: [bold red]Not enough balance[/bold red]:\n\n"
142+
print_error(
143+
"[bold red]Not enough balance[/bold red]:\n\n"
144144
f" balance: [bright_cyan]{proxy_balance}[/bright_cyan]\n"
145145
f" amount: [bright_cyan]{amount}[/bright_cyan]\n"
146146
f" would bring you under the existential deposit: [bright_cyan]{existential_deposit}[/bright_cyan].\n"
147147
f"You can try again with `--allow-death`."
148148
)
149149
return False, None
150150
if account_balance < fee:
151-
err_console.print(
152-
":cross_mark: [bold red]Not enough balance[/bold red]:\n\n"
151+
print_error(
152+
"[bold red]Not enough balance[/bold red]:\n\n"
153153
f" balance: [bright_cyan]{account_balance}[/bright_cyan]\n"
154154
f" fee: [bright_cyan]{fee}[/bright_cyan]\n"
155155
f" would bring you under the existential deposit: [bright_cyan]{existential_deposit}[/bright_cyan].\n"
156156
)
157157
return False, None
158158
if account_balance < amount and allow_death:
159159
print_error(
160-
":cross_mark: [bold red]Not enough balance[/bold red]:\n\n"
160+
"[bold red]Not enough balance[/bold red]:\n\n"
161161
f" balance: [bright_red]{account_balance}[/bright_red]\n"
162162
f" amount: [bright_red]{amount}[/bright_red]\n"
163163
)
164164
return False, None
165165
else:
166166
if account_balance < (amount + fee + existential_deposit) and not allow_death:
167-
err_console.print(
168-
":cross_mark: [bold red]Not enough balance[/bold red]:\n\n"
167+
print_error(
168+
"[bold red]Not enough balance[/bold red]:\n\n"
169169
f" balance: [bright_cyan]{account_balance}[/bright_cyan]\n"
170170
f" amount: [bright_cyan]{amount}[/bright_cyan]\n"
171171
f" for fee: [bright_cyan]{fee}[/bright_cyan]\n"
@@ -175,7 +175,7 @@ async def do_transfer() -> tuple[bool, str, str, Optional[AsyncExtrinsicReceipt]
175175
return False, None
176176
elif account_balance < (amount + fee) and allow_death:
177177
print_error(
178-
":cross_mark: [bold red]Not enough balance[/bold red]:\n\n"
178+
"[bold red]Not enough balance[/bold red]:\n\n"
179179
f" balance: [bright_red]{account_balance}[/bright_red]\n"
180180
f" amount: [bright_red]{amount}[/bright_red]\n"
181181
f" for fee: [bright_red]{fee}[/bright_red]"

bittensor_cli/src/bittensor/subtensor_interface.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from bittensor_cli.src.bittensor.utils import (
3939
format_error_message,
4040
console,
41-
err_console,
41+
print_error,
4242
decode_hex_identity_dict,
4343
validate_chain_endpoint,
4444
u16_normalized_float,
@@ -136,8 +136,8 @@ async def __aenter__(self):
136136
await self.substrate.initialize()
137137
return self
138138
except TimeoutError: # TODO verify
139-
err_console.print(
140-
"\n[red]Error[/red]: Timeout occurred connecting to substrate. "
139+
print_error(
140+
"\nError: Timeout occurred connecting to substrate. "
141141
f"Verify your chain and network settings: {self}"
142142
)
143143
raise typer.Exit(code=1)
@@ -2527,5 +2527,5 @@ async def best_connection(networks: list[str]):
25272527
t2 = time.monotonic()
25282528
results[network] = [t2 - t1, latency, t2 - pt1]
25292529
except Exception as e:
2530-
err_console.print(f"Error attempting network {network}: {e}")
2530+
print_error(f"Error attempting network {network}: {e}")
25312531
return results

0 commit comments

Comments
 (0)