-
Notifications
You must be signed in to change notification settings - Fork 287
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
on error handling #502
Comments
Let me start that I love your feedback and hope to make it into something actionable! I agree that the current error handling situation is less than ideal, mostly due to I can't say that I love the idea of writing explicit error handling in fn main() {
if let Err(err) = run() {
eprintln!("{}", err);
process::exit(1);
}
} and presenting it on nearly every example along with hand crafted error types. This would get rather verbose and would lead the examples to be quite noisy (error boilerplate vs actual example code ratio would suffer). The error boilerplate and The main goals in regard to error handling in cookbook were:
Once the At this point I'm not sure what would be the best way to proceed with the error handling, every approach that you have mentioned is perfectly valid with its own set of drawbacks and each strategy has its usage in given domains If possible I'd like to avoid advertising any particular error handling strategy and hid as much of it as possible, the reader will know that the hidden parts are not part of the example, just the implementation detail for the curious. |
Wow, there's a lot of thought in there that I can appreciate. I personally have seen more and more movement towards a specified Enum of Error variants. I personally like that approach because it defines in the code the error types expected and doesn't require learning a library's macro to use. Here is Hyper's error enum. Here's a stackoverflow answer demonstrating how to implement an Enum. The first book also gives this advice: defining your own error type. I think including something similar in the Error Handling section would help learners understand error-chain under the hood. If at the least we make it difficult for readers of the cookbook to miss doing this once to learn about managing their own errors, a novice would be prepared to use whichever library. I also like the idea of taking What I want to see done is:
|
I think this might come down to folks weighting the negativity of boiler plate differently. To me, adding a 4 line main function to each example in exchange for making the example explicit and easy to understand, is a trade well worth making. Note that the I also think that putting the error_chain! {
foreign_links {
Io(std::io::Error);
}
} My method allows you to remove that and the Separate from that is building out error enums. I do agree that this can become quite noisy. With that said, that pattern is in widespread use in the ecosystem in core libraries. However, it isn't always necessary. |
It's admittedly my very subjective take on how we ought to present the teaching material. We established it without any prior teaching experience mostly based on "gut feeling" so nothing is set in stone and any ideas are welcome :)
Yep we are totally on board with this idea as @AndyGauge mentioned its already on the agenda.
That is true but I'm worried a out two things:
|
I don't think you can really solve this completely. Even if Proper error handling in libraries can be difficult. While I think some examples devoted to it would be great, I do not think using trait objects in the majority of examples is a significant negative. If people wind up using One additional note: |
Hi, I love this cookbook, and I feel fun with typing it on my vim screen :)Thank you.By the way, although the example shows a code which uses 'flate2' and 'tar' library, the code can't be compiled without "error_chain" crate. I found the problem by clicking the "expand" icon and could access the full source codes.It would be better if "error_chain" crate is specified explicitly(and also #[macro_use] is important one), and add some comment about it to avoid confusion to the beginner, or some description which informs reader to click the "expand" icon to get a whole working code.Thanks! |
In the mean time, error-chain is unmaintained, and failure has been deprecated. Yet this cookbook still shows error-chain all while the introduction says:
I think this cookbook should have dedicated recipes about error handling right at the beginning. One for apps and one for libs. I would suggest that they show how to make a custom error type with I would follow the proposition of @BurntSushi of using an explicit main that handles the errors. There's nothing more hampering for learning than some invisible magic happening. Returning For aggregating errors, I know it's easy to have an opinion, but if there was consensus about this, I could put some time towards writing an error handling chapter. |
I wouldn't recommend
|
Are you referring to compilation time? |
I think the cookbooks use of error-chain gives the false impression that error-chain is the current, favoured error-handling library. We could update to use the latest error handling libraries thiserror and/or anyhow, but it's hard to know there won't be a new error handling library in a year or two. For that reason, using only std for error handling becomes desirable. On the other hand, since the rust cookbook is about demonstrating use of the favoured crates in the eco system, perhaps there should be a set of recipes for error handling to show off anyhow and thiserror. Which could mention how you would swap out the std error handling in other reviews for anyhow or thiserror. |
In case it's a useful data-point. I am (comparatively) new to rust, and this is the exact initial impression the use of error_chain here gave me. I did work out that anyhow was more suitable for my needs, but not after losing a little time trying to figure out how to use error_chain, and then realizing that it wasn't state-of-the-art anymore. Definitely something showing off anyhow and thiserror (including why anyhow is often preferred for apps, and thiserror for apis) would have been a lot more instructive for me. |
I realize opinions might run a bit strong here, but I'd like to make a case against the use of
error-chain
(orfailure
) for error handling in the cookbook examples. I think the error handling mechanisms provided by the standard library should be used instead.Some background: I've been using Rust for a long time (since before 1.0), so the cookbook isn't actually something I've ever read in close detail before. I've also never used
error-chain
before (which I think probably reveals some bias in my argument here, FWIW).I have an experience report from a colleague of mine that is in the process of learning Rust. They were using this cookbook as a way of reading example code. One of the things they did in their own code was copy the use of
Result<()>
. But their code wouldn't compile and they couldn't figure out why. When they asked me, I said, "oh, you're probably missing atype Result<T> = ...;
somewhere in your code." But then they pointed me to the example in which they sawResult<()>
, which does not have any such type alias.It took me at least a few minutes of perplexed digging before I was actually able to figure out how that code was compiling. It turns out that the
error_chain!
macro is defining aResult
type alias for you. Personally, I think this makes learning from these examples much harder than necessary. Some might even argue that theResult
type alias itself is problematic, but even aside from that argument, I think having it implicitly defined and brought into scope is really quite surprising, especially for beginners that might be trying to learn Rust through this cookbook.Of course, this is just one experience report. In any case, what are the alternatives? Here are some that I can think of:
error-chain
, but don't use theResult
type alias. (What does one use instead that doesn't suffer from the same implicitness fault?)failure
.unwrap
/panic
.I don't think we should remove proper error handling. I think a cookbook that shows how to do error propagation is much more valuable than a cookbook that doesn't. And I think such examples are incredibly useful for beginners.
I don't think we should use
failure
because its development is a bit in flux, despite it being used in a fair number of crates. In particular, there is a merged RFC to fix the standard library'sError
trait, and this will likely have an impact onfailure
's API. So I think there is a collective "let's wait and see" on that front. However, if and whenfailure
's future becomes solidified, it might be a good idea to transition to using it.I also don't think we should use
error-chain
either, even if we were able to fix the implicitness problem in the examples today. Of course, fixing the implicitness would be something that I'd see as an improvement! For the most part, I perceiveerror-chain
as something that the ecosystem is probably not going to adopt. I think the direction is likely headed more towardsfailure
, or perhaps, evenstd
itself.Ultimately, my argument concludes here that we should just use what the standard library provides. In particular, if I were to write the aforementioned example:
then I would write it like this:
If an example requires dealing with multiple errors, then I'd either write out a new error type, possibly with appropriate
From
impls, or just useBox<Error>
. Namely,Box<Error>
is used in command line tools effectively as a means of bubbling an error up tomain
. The downside to usingBox<Error>
is that it's probably not something one wants to use in a library.I also believe that the examples should not return
Result
directly frommain
, as suggested in #433. I explained my position on that front in more detail here. With that said, I personally feel less strongly on this point than I do about usingerror_chain
orfailure
.Finally, I certainly don't intend to say that
error-chain
(orfailure
) should be expunged from the cookbook entirely. I just think they shouldn't be the go-to error handling mechanism for every single example.Thanks for your consideration! And thanks so much for all your hard work on this wonderful resource. I'd like to end on a positive note: my colleague reports that the cookbook has been a wonderful resource for learning Rust. In particular, he found that the cookbook---above other docs---made it much easier to learn how to actually start writing a full program. That's an awesome compliment!
The text was updated successfully, but these errors were encountered: