From e5637b1dcaf6282e159dce5299a6a365df20454c Mon Sep 17 00:00:00 2001 From: Glenn Willen Date: Tue, 26 Feb 2019 18:00:10 -0800 Subject: [PATCH] wallet: Improve log output for errors during load When loading the default wallet, display the wallet name in error messages as "[default wallet]", instead of the empty string which gives confusing messages. When an exception occurs during wallet loading, display e.what() if possible, instead of nothing. --- src/wallet/wallet.cpp | 16 ++++++++-------- src/wallet/walletdb.cpp | 9 +++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 388422bec84d39..03d3c56caaa93e 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4097,7 +4097,7 @@ bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, b std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, const WalletLocation& location, uint64_t wallet_creation_flags) { - const std::string& walletFile = location.GetName(); + const std::string& log_wallet_name = location.GetName().empty() ? "[default wallet]" : location.GetName(); // needed to restore wallet transaction meta data after -zapwallettxes std::vector vWtx; @@ -4108,7 +4108,7 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, std::unique_ptr tempWallet = MakeUnique(chain, location, WalletDatabase::Create(location.GetPath())); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); if (nZapWalletRet != DBErrors::LOAD_OK) { - InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); + InitError(strprintf(_("Error loading %s: Wallet corrupted"), log_wallet_name)); return nullptr; } } @@ -4124,17 +4124,17 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, if (nLoadWalletRet != DBErrors::LOAD_OK) { if (nLoadWalletRet == DBErrors::CORRUPT) { - InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); + InitError(strprintf(_("Error loading %s: Wallet corrupted"), log_wallet_name)); return nullptr; } else if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR) { InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data" " or address book entries might be missing or incorrect."), - walletFile)); + log_wallet_name)); } else if (nLoadWalletRet == DBErrors::TOO_NEW) { - InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME))); + InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), log_wallet_name, _(PACKAGE_NAME))); return nullptr; } else if (nLoadWalletRet == DBErrors::NEED_REWRITE) @@ -4143,7 +4143,7 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, return nullptr; } else { - InitError(strprintf(_("Error loading %s"), walletFile)); + InitError(strprintf(_("Error loading %s"), log_wallet_name)); return nullptr; } } @@ -4235,12 +4235,12 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, walletInstance->ChainStateFlushed(locked_chain->getTipLocator()); } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) { // Make it impossible to disable private keys after creation - InitError(strprintf(_("Error loading %s: Private keys can only be disabled during creation"), walletFile)); + InitError(strprintf(_("Error loading %s: Private keys can only be disabled during creation"), log_wallet_name)); return NULL; } else if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { LOCK(walletInstance->cs_KeyStore); if (!walletInstance->mapKeys.empty() || !walletInstance->mapCryptedKeys.empty()) { - InitWarning(strprintf(_("Warning: Private keys detected in wallet {%s} with disabled private keys"), walletFile)); + InitWarning(strprintf(_("Warning: Private keys detected in wallet {%s} with disabled private keys"), log_wallet_name)); } } diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 2783f83fd6070a..bb46ec03934cd2 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -422,8 +422,17 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, strType != "minversion" && strType != "acentry") { wss.m_unknown_records++; } + } catch (const std::exception& e) + { + if (strErr.empty()) { + strErr = e.what(); + } + return false; } catch (...) { + if (strErr.empty()) { + strErr = "Caught unknown exception in ReadKeyValue"; + } return false; } return true;