Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Attribute for delegating Error impl to one field #40

Closed
dtolnay opened this issue Nov 16, 2019 · 1 comment · Fixed by #50
Closed

Attribute for delegating Error impl to one field #40

dtolnay opened this issue Nov 16, 2019 · 1 comment · Fixed by #50

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 16, 2019

Something to easily delegate the Error trait's source and backtrace methods and the Display impl to a field. This could be useful for hiding error variants from a library's public error type.

use thiserror::Error;

#[derive(Error, Debug)]
#[error(transparent)]  // <--
pub struct Error(ErrorKind);

#[derive(Error, Debug)]
enum ErrorKind {
    #[error("...")]
    E0,
    #[error("...")]
    E1(#[source] io::Error),
}
@dtolnay
Copy link
Owner Author

dtolnay commented Nov 23, 2019

Also useful for enums that need an "anything else" variant.

#[derive(Error, Debug)]
pub enum MyError {
    ...

    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

Such variants would tend not to have their own Display message but just forward through to the underlying error's Display (and source).

They might hold a backtrace of their own though.

@dtolnay dtolnay changed the title Consider an attribute for delegating Error impl to one field Attribute for delegating Error impl to one field Dec 3, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant