Skip to content

Commit

Permalink
Merge pull request #413 from multiversx/catch-api-errors
Browse files Browse the repository at this point in the history
Handle proxy error
  • Loading branch information
popenta authored Feb 20, 2024
2 parents df972c0 + b84e31c commit 7e477e9
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
3 changes: 2 additions & 1 deletion multiversx_sdk_cli/cli_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
load_password)
from multiversx_sdk_cli.constants import (DEFAULT_TX_VERSION,
TRANSACTION_OPTIONS_TX_GUARDED)
from multiversx_sdk_cli.custom_network_provider import CustomNetworkProvider
from multiversx_sdk_cli.errors import ArgumentsNotProvidedError
from multiversx_sdk_cli.interfaces import ITransaction
from multiversx_sdk_cli.ledger.ledger_functions import do_get_ledger_address
Expand Down Expand Up @@ -249,7 +250,7 @@ def check_options_for_guarded_tx(options: int):


def send_or_simulate(tx: ITransaction, args: Any, dump_output: bool = True) -> CLIOutputBuilder:
proxy = ProxyNetworkProvider(args.proxy)
proxy = CustomNetworkProvider(args.proxy)

is_set_wait_result = hasattr(args, "wait_result") and args.wait_result
is_set_send = hasattr(args, "send") and args.send
Expand Down
8 changes: 3 additions & 5 deletions multiversx_sdk_cli/cli_transactions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from pathlib import Path
from typing import Any, List

from multiversx_sdk_network_providers.proxy_network_provider import \
ProxyNetworkProvider

from multiversx_sdk_cli import cli_shared, utils
from multiversx_sdk_cli.cli_output import CLIOutputBuilder
from multiversx_sdk_cli.cosign_transaction import cosign_transaction
from multiversx_sdk_cli.custom_network_provider import CustomNetworkProvider
from multiversx_sdk_cli.errors import NoWalletProvided
from multiversx_sdk_cli.transactions import (compute_relayed_v1_data,
do_prepare_transaction,
Expand Down Expand Up @@ -88,9 +86,9 @@ def send_transaction(args: Any):

tx = load_transaction_from_file(args.infile)
output = CLIOutputBuilder()
proxy = CustomNetworkProvider(args.proxy)

try:
proxy = ProxyNetworkProvider(args.proxy)
tx_hash = proxy.send_transaction(tx)
output.set_emitted_transaction_hash(tx_hash)
finally:
Expand All @@ -101,7 +99,7 @@ def send_transaction(args: Any):
def get_transaction(args: Any):
args = utils.as_object(args)
omit_fields = cli_shared.parse_omit_fields_arg(args)
proxy = ProxyNetworkProvider(args.proxy)
proxy = CustomNetworkProvider(args.proxy)

transaction = proxy.get_transaction(args.hash, True)
output = CLIOutputBuilder().set_transaction_on_network(transaction, omit_fields).build()
Expand Down
1 change: 1 addition & 0 deletions multiversx_sdk_cli/cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def wallet_new(args: Any):

mnemonic = Mnemonic.generate()
print(f"Mnemonic: {mnemonic.get_text()}")
print(f"Wallet address: {mnemonic.derive_key().generate_public_key().to_address(address_hrp).to_bech32()}")

if format is None:
return
Expand Down
36 changes: 36 additions & 0 deletions multiversx_sdk_cli/custom_network_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Any, Dict, Optional, Protocol

from multiversx_sdk_network_providers import GenericError, ProxyNetworkProvider

from multiversx_sdk_cli.errors import ProxyError
from multiversx_sdk_cli.interfaces import ISimulateResponse, ITransaction


class ITransactionOnNetwork(Protocol):
hash: str
is_completed: Optional[bool]

def to_dictionary(self) -> Dict[str, Any]:
...


class CustomNetworkProvider:
def __init__(self, url: str) -> None:
self._provider = ProxyNetworkProvider(url)

def send_transaction(self, transaction: ITransaction) -> str:
try:
hash = self._provider.send_transaction(transaction)
return hash
except GenericError as ge:
url = ge.url
message = ge.data.get("error", "")
data = ge.data.get("data", "")
code = ge.data.get("code", "")
raise ProxyError(message, url, data, code)

def get_transaction(self, tx_hash: str, with_process_status: Optional[bool] = False) -> ITransactionOnNetwork:
return self._provider.get_transaction(tx_hash, with_process_status)

def simulate_transaction(self, transaction: ITransaction) -> ISimulateResponse:
return self._provider.simulate_transaction(transaction)
10 changes: 10 additions & 0 deletions multiversx_sdk_cli/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@ def __init__(self, message: str):
class ArgumentsNotProvidedError(KnownError):
def __init__(self, message: str):
super().__init__(message)


class ProxyError(KnownError):
def __init__(self, message: str, url: str, data: str, code: str):
inner = {
"url": url,
"data": data,
"code": code
}
super().__init__(message, inner)
3 changes: 2 additions & 1 deletion multiversx_sdk_cli/tests/test_cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ def test_sign_and_verify_message_with_multi_address_pem(capsys: Any):


def _read_stdout_mnemonic(capsys: Any) -> str:
return _read_stdout(capsys).replace("Mnemonic:", "").strip()
lines = _read_stdout(capsys).split("\n")
return lines[0].replace("Mnemonic:", "").strip()


def _read_stdout(capsys: Any) -> str:
Expand Down
5 changes: 1 addition & 4 deletions multiversx_sdk_cli/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import logging
import time
from typing import Any, Dict, Optional, Protocol, Sequence, TextIO, Tuple
from typing import Any, Dict, Optional, Protocol, TextIO

from multiversx_sdk_core import Address, Transaction, TransactionPayload

Expand Down Expand Up @@ -30,9 +30,6 @@ class INetworkProvider(Protocol):
def send_transaction(self, transaction: ITransaction) -> str:
...

def send_transactions(self, transactions: Sequence[ITransaction]) -> Tuple[int, str]:
...

def get_transaction(self, tx_hash: str, with_process_status: Optional[bool] = False) -> ITransactionOnNetwork:
...

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "multiversx-sdk-cli"
version = "9.5.0"
version = "9.5.1"
authors = [
{ name="MultiversX" },
]
Expand Down

0 comments on commit 7e477e9

Please sign in to comment.