Skip to content

Commit

Permalink
wallet: Improve log output for errors during load
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gwillen committed Feb 27, 2019
1 parent d88f7f8 commit e5637b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/wallet/wallet.cpp
Expand Up @@ -4097,7 +4097,7 @@ bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, b

std::shared_ptr<CWallet> 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<CWalletTx> vWtx;
Expand All @@ -4108,7 +4108,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(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;
}
}
Expand All @@ -4124,17 +4124,17 @@ std::shared_ptr<CWallet> 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)
Expand All @@ -4143,7 +4143,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
return nullptr;
}
else {
InitError(strprintf(_("Error loading %s"), walletFile));
InitError(strprintf(_("Error loading %s"), log_wallet_name));
return nullptr;
}
}
Expand Down Expand Up @@ -4235,12 +4235,12 @@ std::shared_ptr<CWallet> 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));
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/wallet/walletdb.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit e5637b1

Please sign in to comment.