Skip to content

Commit a393147

Browse files
committed
feat: add error messages for CreateWithPersistError Display implementation
1 parent ffa857d commit a393147

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

crates/wallet/src/wallet/persisted.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use core::{
55
ops::{Deref, DerefMut},
66
pin::Pin,
77
};
8+
use std::string::{String, ToString};
89

910
use alloc::boxed::Box;
1011
use chain::Merge;
1112

12-
use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet};
13+
use crate::{descriptor::{calc_checksum, DescriptorError}, ChangeSet, CreateParams, LoadParams, Wallet};
1314

1415
/// Trait that persists [`PersistedWallet`].
1516
///
@@ -363,16 +364,40 @@ pub enum CreateWithPersistError<E> {
363364
impl<E: fmt::Display> fmt::Display for CreateWithPersistError<E> {
364365
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365366
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),
367+
Self::Persist(err) => write!(f, "Persistence error: {}", err),
368+
Self::DataAlreadyExists(changeset) => {
369+
write!(f, "{}", changeset_info(changeset))
370+
}
371+
Self::Descriptor(err) => {
372+
write!(f, "{err}")
373+
}
373374
}
374375
}
375376
}
376377

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

0 commit comments

Comments
 (0)