Return an error instead of a panic in the case of data corruption.

This removes an `expect` call that risked crashing the app in the case of
database corruption, potentially hiding other bugs.
This commit is contained in:
Kris Nuttycombe 2024-04-01 11:33:48 -06:00
parent cad4bf757b
commit df52aa844d
1 changed files with 21 additions and 9 deletions

View File

@ -144,15 +144,27 @@ fn account_id_from_ffi<P: Parameters>(
.filter_map(|account_id| {
db_data
.get_account(account_id)
.transpose()
.expect("account_id exists")
.map(|account| match account.source() {
AccountSource::Derived { account_index, .. }
if account_index == requested_account_index =>
{
Some(account)
}
_ => None,
.map_err(|e| {
anyhow!(
"Database error encountered retrieving account {:?}: {}",
account_id,
e
)
})
.and_then(|acct_opt| {
acct_opt
.ok_or(anyhow!(
"Wallet data corrupted: unable to retrieve account data for account {:?}",
account_id
))
.map(|account| match account.source() {
AccountSource::Derived { account_index, .. }
if account_index == requested_account_index =>
{
Some(account)
}
_ => None,
})
})
.transpose()
});