Repository: bitcoin/bitcoin
File: src/wallet/wallet.cpp (C++)

Code before:
    // otherwise just for transaction history.
    CWalletTx* wtx = AddToWallet(tx, TxStateInactive{}, [&](CWalletTx& wtx, bool new_tx) {
        CHECK_NONFATAL(wtx.mapValue.empty());
        CHECK_NONFATAL(wtx.vOrderForm.empty());
        wtx.mapValue = std::move(mapValue);
        wtx.vOrderForm = std::move(orderForm);
        return true;
    });

    // wtx can only be null if the db write failed.
    if (!wtx) {
        throw std::runtime_error(std::string(__func__) + ": Wallet db error, transaction commit failed");
    }

    // Notify that old coins are spent
    for (const CTxIn& txin : tx->vin) {
        CWalletTx &coin = mapWallet.at(txin.prevout.hash);
        coin.MarkDirty();
        NotifyTransactionChanged(coin.GetHash(), CT_UPDATED);
    }

    if (!fBroadcastTransactions) {
        // Don't submit tx to the mempool
        return;
    }

    std::string err_string;
    if (!SubmitTxMemoryPoolAndRelay(*wtx, err_string, node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL)) {
        WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
        // TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
    }
}

DBErrors CWallet::LoadWallet()
{
    LOCK(cs_wallet);

    Assert(m_spk_managers.empty());
    Assert(m_wallet_flags == 0);
    DBErrors nLoadWalletRet = WalletBatch(GetDatabase()).LoadWallet(this);
    if (nLoadWalletRet == DBErrors::NEED_REWRITE)
    {
        if (GetDatabase().Rewrite())
        {
            for (const auto& spk_man_pair : m_spk_managers) {
                spk_man_pair.second->RewriteDB();
            }
        }
    }

    if (m_spk_managers.empty()) {

Review comment (bug):
```suggestion
    if (nLoadWalletRet != DBErrors::LOAD_OK) {
        const auto wallet_file = m_database->Filename();
        
        switch (nLoadWalletRet) {
            case DBErrors::CORRUPT:
                error = strprintf(_("Error loading %s: Wallet corrupted"), wallet_file);
                break;
            case DBErrors::NONCRITICAL_ERROR:
                warnings.push_back(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
                                           " or address metadata may be missing or incorrect."), wallet_file));
                break;
            case DBErrors::TOO_NEW:
                error = strprintf(_("Error loading %s: Wallet requires newer version of %s"), wallet_file, CLIENT_NAME);
                break;
            case DBErrors::EXTERNAL_SIGNER_SUPPORT_REQUIRED:
                error = strprintf(_("Error loading %s: External signer wallet being loaded without external signer support compiled"), wallet_file);
                break;
            case DBErrors::NEED_REWRITE:
                error = strprintf(_("Wallet needed to be rewritten: restart %s to complete"), CLIENT_NAME);
                break;
            case DBErrors::NEED_RESCAN:
                warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
                                           " Rescanning wallet."), wallet_file));
                break;
            case DBErrors::UNKNOWN_DESCRIPTOR:
                error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
                                "The wallet might had been created on a newer version.\n"
                                "Please try running the latest software version.\n"), wallet_file);
                break;
            case DBErrors::UNEXPECTED_LEGACY_ENTRY:
                error = strprintf(_("Unexpected legacy entry in descriptor wallet found. Loading wallet %s\n\n"
                                "The wallet might have been tampered with or created with malicious intent.\n"), wallet_file);
                break;
            case DBErrors::LEGACY_WALLET:
                error = strprintf(_("Error loading %s: Wallet is a legacy wallet. Please migrate to a descriptor wallet using the migration tool (migratewallet RPC)."), wallet_file);
                break;
            default:
                error = strprintf(_("Error loading %s"), wallet_file);
                break;
        }
    }
```

Code after:
        break;
    case DBErrors::NONCRITICAL_ERROR:
        warnings.push_back(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
                                       " or address metadata may be missing or incorrect."),
            wallet_file));
        break;
    case DBErrors::NEED_RESCAN:
        warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
                                       " Rescanning wallet."), wallet_file));
        break;
    case DBErrors::CORRUPT:
        error = strprintf(_("Error loading %s: Wallet corrupted"), wallet_file);
        break;
    case DBErrors::TOO_NEW:
        error = strprintf(_("Error loading %s: Wallet requires newer version of %s"), wallet_file, CLIENT_NAME);
        break;
    case DBErrors::EXTERNAL_SIGNER_SUPPORT_REQUIRED:
        error = strprintf(_("Error loading %s: External signer wallet being loaded without external signer support compiled"), wallet_file);
        break;
    case DBErrors::NEED_REWRITE:
        error = strprintf(_("Wallet needed to be rewritten: restart %s to complete"), CLIENT_NAME);
        break;
    case DBErrors::UNKNOWN_DESCRIPTOR:
        error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
                            "The wallet might have been created on a newer version.\n"
                            "Please try running the latest software version.\n"), wallet_file);
        break;
    case DBErrors::UNEXPECTED_LEGACY_ENTRY:
        error = strprintf(_("Unexpected legacy entry in descriptor wallet found. Loading wallet %s\n\n"
                            "The wallet might have been tampered with or created with malicious intent.\n"), wallet_file);
        break;
    case DBErrors::LEGACY_WALLET:
        error = strprintf(_("Error loading %s: Wallet is a legacy wallet. Please migrate to a descriptor wallet using the migration tool (migratewallet RPC)."), wallet_file);
        break;
    case DBErrors::LOAD_FAIL:
        error = strprintf(_("Error loading %s"), wallet_file);
        break;
    } // no default case, so the compiler can warn about missing cases
    return nLoadWalletRet;
}

util::Result<void> CWallet::RemoveTxs(std::vector<Txid>& txs_to_remove)
{
    AssertLockHeld(cs_wallet);
    bilingual_str str_err;  // future: make RunWithinTxn return a util::Result
    bool was_txn_committed = RunWithinTxn(GetDatabase(), /*process_desc=*/"remove transactions", [&](WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) {
        util::Result<void> result{RemoveTxs(batch, txs_to_remove)};
        if (!result) str_err = util::ErrorString(result);
        return result.has_value();
    });
    if (!str_err.empty()) return util::Error{str_err};