3 ms·
> Rust has three main error systems (io::Error, thiserror, and anyhow), which is a pain when you have to pass them upward through a chain of calls anyhow expli
by swiftcoder 4mo ago
> Rust has three main error systems (io::Error, thiserror, and anyhow), which is a pain when you have to pass them upward through a chain of calls
anyhow explicitly isn't designed for what you are trying to do here. It's designed to be the last link in the chain (and complementary to thiserror, not in competition). If you are using anyhow any deeper than your top-level binary crate, you are likely to be in for an unpleasant time.
- IshKebab 4mo agoHe was talking about the chain of function calls not crates. You still have that in your top level crate.
- wtetzner 4mo agoExcept anyhow is compatible in that direction. io::Error and thiserror types will automatically convert to anyhow errors.
- IshKebab 4mo agoIn my experience it's not quite as automatic as you'd hope. I always often to throw in stuff like `.map_err(Into::into)` or `Ok(foo?)` to convert the errors. Not the end of the world but I do think defending the proliferation of error types in typical Rust projects is copium.
- wtetzner 4mo agoYes, `?` automatically does `.into()`. There are plenty of reasons you can complain about Rust's error handling, but "proliferation of error types" feels like a misunderstanding of how Rust error handling works in the first place.
- mswphd 4mo agoalso thiserror isn't incompatible with io::Error? all thiserror does is do code generation for "typical" enum errors. The errors it generates are normal rust code though, and the fact that they're generated by thiserror shouldn't really matter?
- ziml77 4mo agoI don't get where the idea comes from that the popular error crates make error handling complicated in Rust. Because you're right, all thiserror is doing is giving you a shorter syntax for writing error enums. You could write the exact same things out by hand if you wanted to, and from the library user's side nothing would change. As for anyhow, if a library ever exposes that, then that's just the author being lazy and not doing errors correctly. It's the equivalent of doing throw Exception("error!") in C#.