Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support using error message from source #12

Closed
MOZGIII opened this issue Oct 11, 2019 · 4 comments
Closed

Support using error message from source #12

MOZGIII opened this issue Oct 11, 2019 · 4 comments

Comments

@MOZGIII
Copy link

MOZGIII commented Oct 11, 2019

I have a very simple enum that just wraps all other error types:

#[derive(Debug)]
pub enum SomeParseFromEnvError {
    InvalidLogFormat(ParseFromEnvError<InvalidLogFormat>),
    InvalidBool(ParseFromEnvError<ParseBoolError>),
    InvalidInfallible(ParseFromEnvError<Infallible>),
}

The Display implementation I have doesn't do anything fancy - it just relies the fmt directly to the wrapped value:

impl std::fmt::Display for SomeParseFromEnvError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SomeParseFromEnvError::InvalidLogFormat(err) => err.fmt(f),
            SomeParseFromEnvError::InvalidBool(err) => err.fmt(f),
            SomeParseFromEnvError::InvalidInfallible(err) => err.fmt(f),
        }
    }
}

I need a simple way to derive the same behavior.
Annotation every field with #[error("{0})] is an option that I currently have to use, but it doesn't expand to the same code I write manually.
Also, #[error("{0})] above every field doesn't look good 😄

@dtolnay
Copy link
Owner

dtolnay commented Oct 11, 2019

I would accept a PR to treat:

#[derive(Error, Debug)]
#[error(...)]
pub enum SomeParseFromEnvError {
    InvalidLogFormat(ParseFromEnvError<InvalidLogFormat>),
    InvalidBool(ParseFromEnvError<ParseBoolError>),
    InvalidInfallible(ParseFromEnvError<Infallible>),
}

as equivalent to:

#[derive(Error, Debug)]
pub enum SomeParseFromEnvError {
    #[error(...)]
    InvalidLogFormat(ParseFromEnvError<InvalidLogFormat>),
    #[error(...)]
    InvalidBool(ParseFromEnvError<ParseBoolError>),
    #[error(...)]
    InvalidInfallible(ParseFromEnvError<Infallible>),
}

Would that work for your use case? You would put #[error("{0}")] once at the top of the enum.


it doesn't expand to the same code I write manually.

Thanks, I filed #13 to track this separately.

@MOZGIII
Copy link
Author

MOZGIII commented Oct 11, 2019

I guess #[error("{0}")] is the problem for me here, how about we introduce #[error_proxy_to_source] or sth?

Having to write it for every line may bothers me less than the this thing: ("{0}")

@dtolnay
Copy link
Owner

dtolnay commented Oct 11, 2019

You can alternatively write #[error("{}", .0)] if that's less bothersome.

I think I wouldn't consider this annoying enough to introduce a new #[error_proxy_to_source] attribute that does the same thing.

@dtolnay
Copy link
Owner

dtolnay commented Oct 11, 2019

Closing in favor of #18.

@dtolnay dtolnay closed this as completed Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants