Skip to content

Commit e3cb6db

Browse files
authored
Merge pull request #525 from multiversx/wrap-ledger-errors
Wrap Ledger error
2 parents 4fe058f + 48d8c49 commit e3cb6db

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

multiversx_sdk_cli/base_transactions_controller.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
TRUE_STR_LOWER,
2323
)
2424
from multiversx_sdk_cli.cosign_transaction import cosign_transaction
25-
from multiversx_sdk_cli.errors import BadUserInput
25+
from multiversx_sdk_cli.errors import BadUserInput, TransactionSigningError
2626
from multiversx_sdk_cli.guardian_relayer_data import GuardianRelayerData
2727
from multiversx_sdk_cli.interfaces import IAccount
2828

@@ -47,7 +47,10 @@ def sign_transaction(
4747
self._set_options_for_hash_signing_if_needed(transaction, sender, guardian, relayer)
4848

4949
if sender:
50-
transaction.signature = sender.sign_transaction(transaction)
50+
try:
51+
transaction.signature = sender.sign_transaction(transaction)
52+
except Exception as e:
53+
raise TransactionSigningError(f"Could not sign transaction: {str(e)}")
5154

5255
self._sign_guarded_transaction_if_guardian(
5356
transaction,
@@ -94,15 +97,21 @@ def _sign_guarded_transaction_if_guardian(
9497
) -> Transaction:
9598
# If the guardian account is provided, we sign locally. Otherwise, we reach for the trusted cosign service.
9699
if guardian:
97-
transaction.guardian_signature = guardian.sign_transaction(transaction)
100+
try:
101+
transaction.guardian_signature = guardian.sign_transaction(transaction)
102+
except Exception as e:
103+
raise TransactionSigningError(f"Could not sign transaction: {str(e)}")
98104
elif transaction.guardian and guardian_service_url and guardian_2fa_code:
99105
cosign_transaction(transaction, guardian_service_url, guardian_2fa_code)
100106

101107
return transaction
102108

103109
def _sign_relayed_transaction_if_relayer(self, transaction: Transaction, relayer: Union[IAccount, None]):
104110
if relayer and transaction.relayer:
105-
transaction.relayer_signature = relayer.sign_transaction(transaction)
111+
try:
112+
transaction.relayer_signature = relayer.sign_transaction(transaction)
113+
except Exception as e:
114+
raise TransactionSigningError(f"Could not sign transaction: {str(e)}")
106115

107116
def _convert_args_to_typed_values(self, arguments: list[str]) -> list[Any]:
108117
args: list[Any] = []

multiversx_sdk_cli/cli_shared.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
ArgumentsNotProvidedError,
3434
BadUsage,
3535
IncorrectWalletError,
36+
LedgerError,
3637
)
3738
from multiversx_sdk_cli.guardian_relayer_data import GuardianRelayerData
3839
from multiversx_sdk_cli.interfaces import IAccount
@@ -346,7 +347,10 @@ def prepare_account(args: Any):
346347
hrp=hrp,
347348
)
348349
elif args.ledger:
349-
return LedgerAccount(address_index=args.sender_wallet_index)
350+
try:
351+
return LedgerAccount(address_index=args.sender_wallet_index)
352+
except Exception as e:
353+
raise LedgerError(str(e))
350354
else:
351355
raise errors.NoWalletProvided()
352356

@@ -402,7 +406,10 @@ def load_guardian_account(args: Any) -> Union[IAccount, None]:
402406
hrp=hrp,
403407
)
404408
elif args.guardian_ledger:
405-
return LedgerAccount(address_index=args.guardian_wallet_index)
409+
try:
410+
return LedgerAccount(address_index=args.guardian_wallet_index)
411+
except Exception as e:
412+
raise LedgerError(str(e))
406413

407414
return None
408415

@@ -533,7 +540,10 @@ def load_relayer_account(args: Any) -> Union[IAccount, None]:
533540
hrp=hrp,
534541
)
535542
elif args.relayer_ledger:
536-
return LedgerAccount(address_index=args.relayer_wallet_index)
543+
try:
544+
return LedgerAccount(address_index=args.relayer_wallet_index)
545+
except Exception as e:
546+
raise LedgerError(str(e))
537547

538548
return None
539549

multiversx_sdk_cli/errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,13 @@ def __init__(self, message: str):
149149
class InvalidArgumentsError(KnownError):
150150
def __init__(self, message: str):
151151
super().__init__(message)
152+
153+
154+
class LedgerError(KnownError):
155+
def __init__(self, message: str):
156+
super().__init__(message)
157+
158+
159+
class TransactionSigningError(KnownError):
160+
def __init__(self, message: str):
161+
super().__init__(message)

0 commit comments

Comments
 (0)