From 25d54accf806a3c2d4b0a670580798edac646ad7 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 17 Aug 2021 12:17:45 -0400 Subject: [PATCH] return api errors from `wallet_add` and `wallet_create` --- lbry/error/README.md | 3 +++ lbry/error/__init__.py | 21 +++++++++++++++++++++ lbry/extras/daemon/daemon.py | 10 +++++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lbry/error/README.md b/lbry/error/README.md index cc251a55a..b8684973d 100644 --- a/lbry/error/README.md +++ b/lbry/error/README.md @@ -62,6 +62,9 @@ Code | Name | Message 432 | ServerPaymentWalletLocked | Cannot spend funds with locked wallet, skipping payment round. 433 | ServerPaymentFeeAboveMaxAllowed | Daily server fee of {daily_fee} exceeds maximum configured of {max_fee} LBC. 434 | WalletNotLoaded | Wallet {wallet_id} is not loaded. +435 | WalletAlreadyLoaded | Wallet {wallet_path} is already loaded. +436 | WalletNotFound | Wallet not found at {wallet_path}. +437 | WalletAlreadyExists | Wallet {wallet_path} already exists, use `wallet_add` to load it. **5xx** | Blob | **Blobs** 500 | BlobNotFound | Blob not found. 501 | BlobPermissionDenied | Permission denied to read blob. diff --git a/lbry/error/__init__.py b/lbry/error/__init__.py index e5e819901..986e4e95a 100644 --- a/lbry/error/__init__.py +++ b/lbry/error/__init__.py @@ -278,6 +278,27 @@ class WalletNotLoadedError(WalletError): super().__init__(f"Wallet {wallet_id} is not loaded.") +class WalletAlreadyLoadedError(WalletError): + + def __init__(self, wallet_path): + self.wallet_path = wallet_path + super().__init__(f"Wallet {wallet_path} is already loaded.") + + +class WalletNotFoundError(WalletError): + + def __init__(self, wallet_path): + self.wallet_path = wallet_path + super().__init__(f"Wallet not found at {wallet_path}.") + + +class WalletAlreadyExistsError(WalletError): + + def __init__(self, wallet_path): + self.wallet_path = wallet_path + super().__init__(f"Wallet {wallet_path} already exists, use `wallet_add` to load it.") + + class BlobError(BaseError): """ **Blobs** diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index 52505fd05..7a5aaf055 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -36,7 +36,7 @@ from lbry.blob_exchange.downloader import download_blob from lbry.dht.peer import make_kademlia_peer from lbry.error import ( DownloadSDTimeoutError, ComponentsNotStartedError, ComponentStartConditionNotMetError, - CommandDoesNotExistError, BaseError + CommandDoesNotExistError, BaseError, WalletNotFoundError, WalletAlreadyLoadedError, WalletAlreadyExistsError ) from lbry.extras import system_info from lbry.extras.daemon import analytics @@ -1276,9 +1276,9 @@ class Daemon(metaclass=JSONRPCServerType): wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id) for wallet in self.wallet_manager.wallets: if wallet.id == wallet_id: - raise Exception(f"Wallet at path '{wallet_path}' already exists and is loaded.") + raise WalletAlreadyLoadedError(wallet_path) if os.path.exists(wallet_path): - raise Exception(f"Wallet at path '{wallet_path}' already exists, use 'wallet_add' to load wallet.") + raise WalletAlreadyExistsError(wallet_path) wallet = self.wallet_manager.import_wallet(wallet_path) if not wallet.accounts and create_account: @@ -1311,9 +1311,9 @@ class Daemon(metaclass=JSONRPCServerType): wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id) for wallet in self.wallet_manager.wallets: if wallet.id == wallet_id: - raise Exception(f"Wallet at path '{wallet_path}' is already loaded.") + raise WalletAlreadyLoadedError(wallet_path) if not os.path.exists(wallet_path): - raise Exception(f"Wallet at path '{wallet_path}' was not found.") + raise WalletNotFoundError(wallet_path) wallet = self.wallet_manager.import_wallet(wallet_path) if self.ledger.network.is_connected: for account in wallet.accounts: