Help needed for error stratergy inside Mercurial
Hi all,
I've been having an ergonomic issue with error handling lately. Error
handling strategy used to be kind of a rough edge in Rust because of the
different constraints of both the compiler and the standard library.
Lately it seems that most projects are happy with using a mix of
thiserror/anyhow/eyere/snafu, however when investigating using
(a potential combination) of them, they don't appear to allow for all
the constraints that we have.
Rust errors in the Mercurial/hg codebase:
- need to be matchable at the library layers (basically that everything
inside
hg-coreexposes enums) - need to carry enough information for end-users to make sense of the issue and/or devs to be able to help end-users with it
- need to match the Python implementation's user-facing behavior basically exactly (we can have some leeway for OS errors, I guess, but the point is to leave the formatting to us and not a library)
- shouldn't do any formatting inside
hg-core, that should be left to consumers ofhg-core(though having a default formatting isn't a bad idea for debugging) - shouldn't assume that data is somehow UTF8 (ties in to the last
point), as we have non-UTF8 data we need to format using
format_bytesor equivalent (no popular crate seems to care since it's a pretty niche need) - need to compose somehow, since not all code can be perfectly
hierarchical: the
dirstatemodule needs to call functions that may return aRevsetErrorand therevsetmodule needs to call functions that may return aDirstateError, creating cycles in error enums (this is the big one for me that started this refactor and questioning).
So I'm calling to the Rust experts, or at least people that have some experience with working in larger Rust codebases about what the optimal strategy would be.
Thank you for your time
Posted Aug 22, 2022, 9:11 AM UTC
IMO, just use thiserror (if you need to match the errors and you're a library anyway, then you should avoid anyhow. If you want to do your own formatting, I think you are better off not building any reporting functionality into your errors and handling that separately, i.e., not the Eyre approach. I must admit I don't understand the benefits of the Snafu approach though). Design errors to fit logically with the domain, rather than one per module. Use lots of From impls to make it easy to convert between error types. Simple nesting of errors inside another error is really easy, but often the wrong thing to do. In particular for your last requirement, it leads to these cycles. Instead, error conversion should convert the error and make it appropriate for the new context. You may need to do a lot of normalisation of errors too (e.g., If you have error types A and B and have A::Foo(B) (that is, a B nested in an A error), then if you try to convert that to a B, you should aboslutely not end up with B::Bar(A), you should either get the wrapped up B or a third kind of error).
If you keep the error handling stuff just about carrying data and don't try to prematurely format your errors (or rely too much on the dervied Display impls) then the non UTF8 thing shouldn't be an issue. I.e., keep error formatting/reporting very separate from error handling in Rust code (unfortunately the ergonomics of libraries lead you away from this, but be strong!)
Thank you for taking the time to answer. This makes sense to me, as it was my current best guess, though I needed more people (like you and u/matklad) to tell me that this is indeed a good plan, and what pitfalls to look for. Hopefully I can make sense of this all pretty soon, thanks again.