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

Improve #[from] documentation example. #135

Open
Shaddy opened this issue May 26, 2021 · 4 comments
Open

Improve #[from] documentation example. #135

Shaddy opened this issue May 26, 2021 · 4 comments

Comments

@Shaddy
Copy link

Shaddy commented May 26, 2021

Hey folks,

There is an issue that took me a while to understand, and it's that in the lib doc is not very clear how to display the error that is being derived with #[from] macro.

The actual example is e.g.

    #[error("data store disconnected")]
    Disconnect(#[from] io::Error)

Despite there is also an example of how to use from attribute over a struct field e.g.

#[derive(Error, Debug)]
pub enum MyError {
    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    },
}

None of these examples tells you can do it e.g.

    #[error("data store disconnected: {0}")]
    Disconnect(#[from] io::Error)

Would be great to have it in the doc.

Thanks!

@PSteinhaus
Copy link

Thank you, I pretty much looked at the repo just hoping to find something like this.

@Altair-Bueno
Copy link

Took me a while to figure it out too. Should be included on README

@DJDuque
Copy link

DJDuque commented Aug 2, 2022

Even though it works, should this be encouraged by the documentation? I don't think that this is the idiomatic way to print this error. Using #from automatically implements source; then this error should be printed like:

println!("Error: {error}");
println!("Caused by: {}", error.source().unwrap());

I guess this is how most people would expect this to behave, and they would get:

Error: data store disconnected: inner reason of failure
Caused by: inner reason of failure

Maybe an example using source is more appropriate?

@failable
Copy link

Any answer from 2024?

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

5 participants