4 ms·
That's a good approach if you don't care about the nature of error and plan on logging it or outputting it as is, but you lose any metadata for the error unless
by onei 6y ago
That's a good approach if you don't care about the nature of error and plan on logging it or outputting it as is, but you lose any metadata for the error unless you want to use unsafe to get it back out. What I order to do is use pattern matching for control flow in error handling, e.g. was this a server error or was I given something invalid? Depending on the error, I might want to try again.
With Python, you can create a rich hierarchy of errors using inheritance, but to achieve something similar in Rust the best option I've come up with is maintaining an enum that wraps the different error types. It feels clunky, so I hope there's better ways.
- saghm 6y agoIf you're interested in generating a lot of the boilerplate for an error enum, I'd highly recommend `thiserror` (https://crates.io/crates/thiserror https://crates.io/crates/thiserror). It's made by the same person who made a lot of the other prominent proc macros libraries in the Rust community (e.g. serde).
- onei 6y agoThat looks exactly like what I'm looking for! Many thanks for the recommendation.