Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle proxy error #413

Merged
merged 4 commits into from
Feb 20, 2024
Merged

Handle proxy error #413

merged 4 commits into from
Feb 20, 2024

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Feb 19, 2024

Handle proxy error and display address of newly created wallet.

Fixes #412.

@popenta popenta self-assigned this Feb 19, 2024
schimih
schimih previously approved these changes Feb 19, 2024
Comment on lines 277 to 279
message = ge.data["error"]
data = ge.data["data"]
code = ge.data["code"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case, perhaps use ge.get("...", "")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -198,3 +198,13 @@ def __init__(self, message: str):
class ArgumentsNotProvidedError(KnownError):
def __init__(self, message: str):
super().__init__(message)


class ProxyError(KnownError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 136 to 143
try:
tx_hash = proxy.send_transaction(payload)
except GenericError as ge:
url = ge.url
message = ge.data["error"]
data = ge.data["data"]
code = ge.data["code"]
raise ProxyError(message, url, data, code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have this in several places now, perhaps use the decorator pattern, and do a custom proxy provider e.g. CustomNetworkProvider(ProxyNetworkProvider) that overrides this function, catches & re-throws?

Alternatively, catch multiversx_sdk_network_providers.GenericError in cli.py, in the main function, and handle it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a CustomNetworkProvider.

@@ -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()}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, not a breaking change for some users 🤞

People who rely on a more structured output should use the outfile argument.

utils.dump_out_json(output, outfile=args.outfile)
tx_hash = proxy.send_transaction(tx)
output.set_emitted_transaction_hash(tx_hash)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try / finally was good here - so that the user sees the emitted transaction even if the send fails with exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Added.

@popenta popenta merged commit 7e477e9 into main Feb 20, 2024
11 checks passed
@popenta popenta deleted the catch-api-errors branch February 20, 2024 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Catch API errors as "KnownError" and display them more nicely
3 participants