Source display in a error display

In thiserror example there's no underlying error details in Display if there's a source:

    #[error("data store disconnected")]
    Disconnect(#[from] io::Error),
    #[error("the data for key `{0}` is not available")]
    Redaction(String),

Disconnect has a source so its Display is just a static string, while Redaction doesn't have a source and it's details is added to Display.

But it other libs I see the source details passed into Display e.g. in sqlx error:

    #[error("error with configuration: {0}")]
    Configuration(#[source] BoxDynError),

What's the benefits of the redundancy of the second approach?

With the second form you can do println!("{}", error) and see the full context without needing to iterate through the sources manually (e.g. "error with configuration: unable to open config file: permission denied").

I personally don't like it because libraries like anyhow will print the full error chain anyway, meaning you'd get duplicates in your error messages:

Error: error with configuration: unable to open config file: permission denied
  Caused By: unable to open config file: permission denied
  Caused By: permission denied

In comparison to the non-redundant form:

Error: error with configuration
  Caused By: unable to open config file
  Caused By: permission denied
1 Like

Like Michael, I'd prefer something more like:

#[error("error with configuration")]
Configuration(#[source] BoxDynError),

As an aside, I'm not a fan of implementing from for public error types. Especially not for something as public and generic as io::Error. The source is something you have more control over and doesn't simply allow creating, say, a Disconnect error from any random io::Error.

So for example, using source you can have multiple errors based on io::Error that provide a bit more context. E.g.:

#[derive(Error, Debug)]
pub enum DataStoreError {
    #[error("data store disconnected")]
    Disconnect(#[source] io::Error),
    #[error("could not read from data store")]
    Read(#[source] io::Error)
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.