Skip to content

Commit 0ae48b1

Browse files
committed
feat: add error messages for CreateWithPersistError Display implementation
1 parent b37355a commit 0ae48b1

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

wallet/src/wallet/persisted.rs

+37-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ use core::{
66
pin::Pin,
77
};
88

9-
use alloc::boxed::Box;
9+
use alloc::{
10+
boxed::Box,
11+
string::{String, ToString},
12+
};
1013
use chain::Merge;
1114

12-
use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet};
15+
use crate::{
16+
descriptor::{calc_checksum, DescriptorError},
17+
ChangeSet, CreateParams, LoadParams, Wallet,
18+
};
1319

1420
/// Trait that persists [`PersistedWallet`].
1521
///
@@ -363,16 +369,38 @@ pub enum CreateWithPersistError<E> {
363369
impl<E: fmt::Display> fmt::Display for CreateWithPersistError<E> {
364370
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365371
match self {
366-
Self::Persist(err) => fmt::Display::fmt(err, f),
367-
Self::DataAlreadyExists(changeset) => write!(
368-
f,
369-
"Cannot create wallet in persister which already contains wallet data: {:?}",
370-
changeset
371-
),
372-
Self::Descriptor(err) => fmt::Display::fmt(&err, f),
372+
Self::Persist(err) => write!(f, "Persistence error: {}", err),
373+
Self::DataAlreadyExists(changeset) => {
374+
write!(f, "{}", changeset_info(changeset))
375+
}
376+
Self::Descriptor(err) => {
377+
write!(f, "{err}")
378+
}
373379
}
374380
}
375381
}
376382

383+
/// Helper function to write basic fingerprinting information about a changeset
384+
fn changeset_info(changeset: &ChangeSet) -> String {
385+
let network = match &changeset.network {
386+
Some(network) => network.to_string(),
387+
None => "None".to_string(),
388+
};
389+
let external_checksum = match &changeset.descriptor {
390+
Some(descriptor) => calc_checksum(&descriptor.to_string()).unwrap(),
391+
None => "[no external descriptor in the changeset]".to_string(),
392+
};
393+
let internal_checksum = match &changeset.change_descriptor {
394+
Some(descriptor) => calc_checksum(&descriptor.to_string()).unwrap(),
395+
None => "[no internal descriptor in the changeset]".to_string(),
396+
};
397+
398+
format!(
399+
"Cannot create wallet in a persister which already contains wallet data: \
400+
Network: {}, External Descriptor Checksum: {}, Internal Descriptor Checksum: {}",
401+
network, external_checksum, internal_checksum
402+
)
403+
}
404+
377405
#[cfg(feature = "std")]
378406
impl<E: fmt::Debug + fmt::Display> std::error::Error for CreateWithPersistError<E> {}

0 commit comments

Comments
 (0)