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

Add example for expressions in error attribute #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dnaka91
Copy link

@dnaka91 dnaka91 commented Jul 19, 2022

Hi @dtolnay, that you so much for this awesome crate. I use it everywhere all the time (together with anyhow).

Was trying to figure out how to nicely display an Option<T> value in the error message, instead of just using {0:?} and having Some("...")/None in the displayed message.

So I found a nice test case in tests/test_display.rs, and thought it might be nice to add a tiny
example to the README.md and crate docs, to showcase that expressions are possible in the #[error("...")] attribute as well.

Wording of the example could maybe be a bit improved, I'm not as good of a writer as you 🙇‍♂️.

@bjeanes
Copy link

bjeanes commented Aug 29, 2022

@dnaka91 thank you! I came here for exactly this reason. I have an error that comes from an API which always includes an error code and usually but not always includes a clarifying error message. Your example helped me a lot.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjeanes would you be able to share the code that you ended up writing based on this? I think there may be room to do a more compelling example than what's currently in the PR; I don't find "unknown reason" informative to show in an error message (as opposed to leaving it out).

@dnaka91
Copy link
Author

dnaka91 commented Sep 30, 2022

I'm happy to change my initial sample to something more elaborate. My case was really something that simple where I just wanted to avoid the Some("...")/None as part of the error message.

How about something like a PathBuf + std::io::Error that matches on the error kinds to give nicer messages than the default I/O error implementation?

Of course would be interested in the code from @bjeanes too.

@bjeanes
Copy link

bjeanes commented Sep 30, 2022

Thanks for the bump. I saw the previous notification but got sidetracked digging up my example and forgot!

I have some code which works with an API which returns error payloads like {"code": 301, "message": "read failed"}. The error codes are totally undocumented and only some of them include a message field.

So, my error to represent these looks like this:

#[error("{code}{}", match .message {
    Some(msg) => format!(" - {}", &msg),
    None => "".to_owned(),
})]
SungrowError { code: u16, message: Option<String> },

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

Successfully merging this pull request may close these issues.

None yet

3 participants