Skip to content

Display implementations should not expose Debug output #1933

@casey

Description

@casey

Currently, the Display implementation for LoadError displaysDebug output for LoadError::MissingDescriptor and LoadError::Mismatch:

impl fmt::Display for LoadError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LoadError::Descriptor(e) => e.fmt(f),
            LoadError::MissingNetwork => write!(f, "loaded data is missing network type"),
            LoadError::MissingGenesis => write!(f, "loaded data is missing genesis hash"),
            LoadError::MissingDescriptor(k) => {
                write!(f, "loaded data is missing descriptor for keychain {k:?}")
            }
            LoadError::Mismatch(mismatch) => write!(f, "data mismatch: {mismatch:?}"),
        }
    }
}

I think this is generally not a good practice. Debug output is automatically generated, and is not as nice to read as a hand-written Display implementation's output.

I noticed this in a test we were loading a wallet with the incorrect network, and instead of getting a nice error message, we got data mismatch: Network { loaded: Network::Testnet, expected: Network::Bitcoin }. Our project never exposes Debug output to the user, and we'd like to avoid doing this if we use BDK, but still be able to expose BDK errors to the user via Display.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions