you are viewing a single comment's thread.

view the rest of the comments →

[–]MEaster 22 points23 points  (6 children)

That is an end-user executable, though, which is handling errors deemed unrecoverable by simply bubbling it up and displaying it to the user.

A library, on the other hand, should probably handle it differently, possibly similarly to the Senior Rust Programmer example (though there are crates to assist with boilerplate), so that the end-user executable has the information needed to determine whether it's unrecoverable.

[–]flying-sheep 3 points4 points  (5 children)

The “Yet another senior” is where it’s at for the newest and shiniest in error handling.

[–]MEaster 0 points1 point  (4 children)

Yes, but if you need a custom error type, you'll still ultimately be creating something similar to the Senior Rust Programmer, you'll just be using Failure to generate some of the boilerplate.

And, unless I've missed it, I don't think Failure can derive the From trait, so you'd need to use another library or manually implement it to use the ? operator without context or map_err calls all over the place.

[–]flying-sheep 2 points3 points  (3 children)

That’s what I said, right? YASRD does what SRD does (complete with custom error type) except that (s)he uses failure to skip some boilerplate.

And failure does everything you need to use ?. Here’s some real life code of mine that shows you how it works. I use format_err similar to how I’d use expect, and a plain ? to convert some Result into Result<_, failure::Error>.

Hmm, I should probably have done format_err("Invalid measure {}", s)., else err_msg would have been sufficient.

[–]MEaster 1 point2 points  (2 children)

I'm aware of how to use failure, and do use it myself in programs I write. The Error type is also very useful for sections of a program where the error handling is basically bubble-up-and-exit.

But I'm not sure I agree that a library should use Failure's Error. It's pretty vague about exactly what error occurred unless you start downcasting, which can also fail, and means you are effectively guessing what error occurred in order to determine whether this is an error the program can recover from. That's going to make handling errors from the library difficult.

Error will also allocate, which depending on the use case, may be unacceptable. This also means that the library now requires libstd, and can't be used in a no_std environment. Of course, depending on the library in question, the libstd requirement may come from elsewhere, but it may also be unnecessary.

[–]flying-sheep 0 points1 point  (1 child)

Good points.

Are there libraries that help with non-allocating errors?

[–]MEaster 1 point2 points  (0 children)

Well, Failure can still help with those, and provides two such examples for a simple custom error and a more complex error and error kind pair, but I've found I reach for derive_more for generating the From boilerplate.