Skip to content

Instantly share code, notes, and snippets.

@Enet4
Created August 23, 2020 17:13
Show Gist options
  • Save Enet4/5e59366392d4c087aafa59a833711b78 to your computer and use it in GitHub Desktop.
Save Enet4/5e59366392d4c087aafa59a833711b78 to your computer and use it in GitHub Desktop.

Most examples in the SNAFU guide show the following pattern:

#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
    #[snafu(display("Could not read data set token: {}", source))]
    #[non_exhaustive]
    ReadToken {
        #[snafu(backtrace)]
        source: dicom_parser::dataset::read::Error,
    },
}

Notice how the source field is chained to the tail of the type's display implementation. This makes it so that a single line print is enough to present all information about the error (minus the backtrace).

eprintln!("[ERROR] {}", e);

one possible output:

[ERROR] Could not read data set token: Could not read item value: Undefined value length of element tagged (5533,5533) at position 3548

However, this will not integrate well with other error reporting facilities, such as eyre, which are already prepared to traverse the chain of error causes and show them individually.

If we were to turn our error into an eyre::Report and print that:

eprintln!("[ERROR] {:?}", eyre::Report::from(e));

We get this twisted mess of repetition:

[ERROR] Could not read data set token: Could not read item value: Undefined value length of element tagged (5533,5533) at position 3548

Caused by:
   0: Could not read item value: Undefined value length of element tagged (5533,5533) at position 3548
   1: Undefined value length of element tagged (5533,5533) at position 3548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment