Merge bitcoin/bitcoin#32598: walletdb: Log additional exception error messages for corrupted wallets

ad9a13fc42 walletdb: Log additional exception error messages for corrupted wallets (Ava Chow)

Pull request description:

  Many exceptions thrown for corruption are `std::runtime_error`; we should catch those and log the message to help with debugging.

  Split from #32489

ACKs for top commit:
  davidgumberg:
    ACK ad9a13fc42
  furszy:
    ACK ad9a13fc42
  rkrux:
    ACK ad9a13fc42
  Sjors:
    utACK ad9a13fc42

Tree-SHA512: 107b938d67346804733ea27c44ed38822db0e020e5b1ac889ee35280d812ec56dcc9af7b3eab7a521d72cdd9cb4a8d6d35f3a3dfbcb2a6fd170a981f34fbdfc2
This commit is contained in:
merge-script
2025-05-30 11:33:24 +01:00

View File

@@ -541,7 +541,6 @@ bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch)
std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
if (!cursor) {
// Could only happen on a closed db, which means there is an error in the code flow.
wallet.WalletLogPrintf("Error getting database cursor for '%s' records", type);
throw std::runtime_error(strprintf("Error getting database cursor for '%s' records", type));
}
@@ -1194,9 +1193,15 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
// Load decryption keys
result = std::max(LoadDecryptionKeys(pwallet, *m_batch), result);
} catch (...) {
} catch (std::runtime_error& e) {
// Exceptions that can be ignored or treated as non-critical are handled by the individual loading functions.
// Any uncaught exceptions will be caught here and treated as critical.
// Catch std::runtime_error specifically as many functions throw these and they at least have some message that
// we can log
pwallet->WalletLogPrintf("%s\n", e.what());
result = DBErrors::CORRUPT;
} catch (...) {
// All other exceptions are still problematic, but we can't log them
result = DBErrors::CORRUPT;
}