3 ms·
"And then I learned about error propagation using the question mark operator in Rust:... Arguments can be made about creating custom error messages at each call
by jerf 8y ago
"And then I learned about error propagation using the question mark operator in Rust:... Arguments can be made about creating custom error messages at each call level, but is that really necessary most of the time?"
Actually, one of the things that has prevented Go from "simply" or "just" solving the error propagation problem is precisely the recognition that in real code,
if err != nil {
return err
}
becomes problematic at scale, because it obscures the history of the error. Stack traces are great for humans, but not terribly useful for programmatic interpretation. Personally, I still tend to use "if err != nil { return err }" as my sort of default sketch implementation, but I find it is very common for many of those to grow something else before the program ships, and I'm still not all that great or consistent about layering errors properly anyhow.
I'd suggest to a lot of people that it may be worth reading or re-reading the Go 2 error proposal [1], and to try to come at it with some relatively fresh eyes, rather than reading it for "does this precisely conform to my preconceived notions about what error handling should be?" (Another interesting aspect is around providing some base API for errors, and processing composite errors, rather than expecting end-users to do all the definition [2]. Personally I consider both important.) There is some legitimately interesting discussion and work around what to do, based on waiting to gather a lot of data about error handling before just slamming a solution into place, moreso than may meet the eye. I can't speak to Rust's solution in practice, but having used Haskell for a long time with union types for errors, I can say they are hardly error-handling nirvana there. If I'm reading the Rust documentation correctly, they're going to have the same problems in Rust as they do in Haskell.
[1]: https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md https://go.googlesource.com/proposal/+/master/design/go2draf...
[2]: https://go.googlesource.com/proposal/+/master/design/go2draft-error-values-overview.md https://go.googlesource.com/proposal/+/master/design/go2draf...
- erik_seaberg 8y agoif err != nil { return nil, errors.Wrap(err, "bad stuff during foo") } looks pretty handy, I just want to tell the compiler to infer that everywhere by default, because writing it over and over is not only a waste of time but actively makes all code harder to read.
- jerf 8y agoThat's part of what's going into the new error proposal. (Although it's not going to be at the global level, but at the function level. "At the global level" becomes tricky if you try to actually spec it out; it's easy enough to just slap some designs down, but ones that compose well aren't so easy.)
- vram22 8y agoThat code seems somewhat like Python's chained exceptions.
- ansible 8y ago> I can't speak to Rust's solution in practice, but having used Haskell for a long time with union types for errors, I can say they are hardly error-handling nirvana there. Haskell's Either-like Monads have gone through considerable evolution: https://www.yesodweb.com/blog/2016/04/fixing-monad-either https://www.yesodweb.com/blog/2016/04/fixing-monad-either I'd love to hear more from someone familiar with Haskell and Rust to talk about if Rust's Result enum would have the same kinds of issues they've had in the Haskell world.
- steveklabnik 8y agoIt's been a while since I've actually written haskell, but a lot of this is about monads, both Monad and monad transformers, both of which Rust doesn't have. > The broken fail function We don't have this. > The problem with Error Our Error trait (typeclass in haskell terms) had a "description" method that had some issues, so we've deprecated it and provided a default impl. We expect errors to implement a similar type signature, but with the Display/Debug traits, not some sort of fromString trait. > The proper Monad Either instance not applicable > Either for arbitrary error types Rust's Result already works for arbitrary error types; you don't have to actually implement Error for them, so this is a non-issue.