-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 section on common message styles for Result::expect #96033
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there not a third style here that combines the two, using the "expect as error message" style for the message and simply writing the precondition in a comment similar to a // Safety:
comment before an unsafe
block?
I'm struggling to see the advantages of the "expect as precondition" style given that one would almost certainly look at the offending line when debugging an issue encountered by a user (or oneself) and thus come across the comment explaining the precondition. It removes one layer of indirection (the need to read through the code) but that seems like a fairly small win. I'd be much happier if expect messages would converge towards one style across the Rust ecosystem.
Co-authored-by: Emil Thorenfeldt <[email protected]>
Co-authored-by: Emil Thorenfeldt <[email protected]>
I hadn't seen this style before. I wouldn't want to recommend using "Safety" personally, because I think that should be reserved for unsafe code, but the general idea sounds reasonable to me. I'm certain there are probably other styles beyond even these three, so I imagine this will be a living document as more people come forward and add context that we currently lack. As for advantages, I think it comes down to how you're using expect. So long as you're using For comparison, I setup a couple test cases for each approach and I tossed in the experimental IMO, the only useful information being added in the former style is that it is mentioning the env variable's name, though both styles express this info so it's not an advantage necessarily. Beyond that all the information being expressed is a duplicate of information already present in the original source error. Looking at this though I think I want to go back and apply your second suggestion for how you fixed the typos in my expect precondition message. The "should be" framing seems to fit a lot better than the "is always" one. I think this might be closer to my ideal usage / output, assuming we had the specialized expect that uses let path = std::env::var("IMPORTANT_PATH")
.expect("env variable `IMPORTANT_PATH` should always be set by `wrapper_script.sh`"); With an output something like this... (note: extremely strawman output, I have no idea if we would even be okay with changing the default output of the
|
This comment was marked as outdated.
This comment was marked as outdated.
3a4905e
to
72898ac
Compare
library/core/src/result.rs
Outdated
/// independent from our source error. | ||
/// | ||
/// ```text | ||
/// thread 'main' panicked at 'env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent', src/main.rs:4:6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In an ideal world this example would either use .map_err(std::error::Report::new).expect(...)
to show the proper error message rather than the Debug
output of this source, or, once we land error in core and have landed an expect
equivalent that requires E: Error
we could automatically format this correctly.
For now I'm just letting it print poorly rather than complicating the example with an adapter type to convert Display
to Debug
, which would still be incorrect as a general recommendation if the wrapped error type had a source
that needs to be printed.
library/core/src/result.rs
Outdated
/// In this example we are communicating not only the name of the | ||
/// environment variable that should have been set, but also an explanation | ||
/// for why it should have been set, and we let the source error display as | ||
/// a clear contradiction to our expectation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the strong language here, because it also fits well with a more general thing about saying that .expect
is generally not supposed to be for user-visible stuff (though of course it might happen) since it's for things that you have a reason to expect won't happen, and that reason is what goes in the text. (Said otherwise, result errors says what went wrong, expect messages are for saying why you think it shouldn't have happened.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like all this text, though I had one pandora note.
One thing that's not super obvious to me, though, is whether this makes sense in a method's documentation. That's a long comment, and I'm not sure that having to scroll past it to see all of Result
's methods makes sense.
Maybe there's a way to put a paragraph in the actual method, and merge the long-form discussion into a book somewhere?
I'm not opposed to putting it in the book, and I want this reachable from as many places as possible, but I have an alternative idea. I've been thinking about reclaiming the |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This looks basically good, but it looks like there's an issue in the PR build? If you get that fixed I'll do a last once over and approve. |
yea, working on that and adding more links in the generated html, I'll push a new copy shortly |
@rustbot ready |
Looks good! @bors r+ rollup |
📌 Commit ef879c6 has been approved by |
…askrgr Rollup of 3 pull requests Successful merges: - rust-lang#96033 (Add section on common message styles for Result::expect) - rust-lang#97354 (Updates to browser-ui-test) - rust-lang#97424 (clippy::complexity fixes) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
All expect messages are adjusted or replaced with unwrap following the guidelines here: rust-lang/project-error-handling#50 rust-lang/rust#96033
All expect messages are adjusted or replaced with unwrap following the guidelines here: rust-lang/project-error-handling#50 rust-lang/rust#96033
All expect messages are adjusted or replaced with unwrap following the guidelines here: rust-lang/project-error-handling#50 rust-lang/rust#96033
Based on a question from rust-lang/project-error-handling#50 (comment)
One thing I haven't decided on yet, should I duplicate this section on: I ended up moving the section toOption::expect
, link to this section, or move it somewhere else and link to that location from both docs?std::error
and referencing it from bothResult::expect
andOption::expect
's docs.I think this section, when combined with the similar update I made on
std::panic!
implies that we should possibly more aggressively encourage and support the "expect as precondition" style described in this section. The consensus among the libs team seems to be that panic should be used for bugs, not expected potential failure modes. The "expect as error message" style seems to align better with the panic for unrecoverable errors style where they're seen as normal errors where the only difference is a desire to kill the current execution unit (aka erlang style error handling). I'm wondering if we should be providing a panic hook similar tohuman-panic
or more strongly recommending the "expect as precondition" style of expect message.