r/rust•
Alphare

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-core exposes 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 of hg-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_bytes or 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 dirstate module needs to call functions that may return a RevsetError and the revset module needs to call functions that may return a DirstateError, 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

128
nick29581rustfmt · rust•

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!)

6
AlphareOPmercurial•

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.

2
matkladrust-analyzer•

Formatting is the tough nut here. This is similar to a problem of i18n of error messages. The solution is to make errors just transparent bags of data. This makes the API hard to evolve in a backwards compatible way. Do you also want to have nice semver for hg-core? I think that doing both “nice 1.0 API without breaking changes” and “caller can format error messages” is going to be pretty hard, so pick one!

If you pick formatting, than, yeah, just thiserror the stuff. You want to have not too few and not too many error enums. If there’s mutual dependency between the modules, they probably should have the same error type.

Carefully study all places where you want to match&handle the errors. It’s best if you can design the API such that matching is not needed. Sometimes the following helps:

fn f() -> Result<Result<T, Specific>, General>

Where General is some ambient environmental error which you just ?, and Specific is something the caller matches over.

Be careful with From impls. The are convenient, but often have kitchen soup semantics. The following sometimes is needed:

enum AppError {
  ReadingConfing(io::Error),
  DownloadingData(io::Error),
}

There’s no reasonable From impl here. https://kazlauskas.me/entries/errors is an under-appreciated classic.

3
AlphareOPmercurial•

Do you also want to have nice semver for hg-core? I think that doing both “nice 1.0 API without breaking changes” and “caller can format error messages” is going to be pretty hard, so pick one!

I don't think we'll ever want semver for hg-core, or at least not for a very long time. Stable internal APIs for VCS were tried (Bazaar, and in some ways Git through the filesystem) and it's a nightmare. I can see exception being made for small crates with a very precise delimiter like a hgignore crate, but that's about it. :)

Where General is some ambient environmental error which you just ?, and Specific is something the caller matches over.

Nice trick, I'll keep that in mind where that makes sense.

Be careful with From impls. The are convenient, but often have kitchen soup semantics

Yeah, that's basically what I noticed from having a strategy of "we'll see when we get to more complexity".

There’s no reasonable From impl here. https://kazlauskas.me/entries/errors is an under-appreciated classic

This resonates with my experience, indeed.

Same as I said to u/nick29581, I needed more people to tell me that this is indeed a good plan, and what pitfalls to look for.

Thanks again!

2
lebensterben•

where is your repo

2
AlphareOPmercurial•
3
occamatl•

I see that the rhg sub-project is a no-Python re-implementation of Mercurial (as opposed to just creating Python extensions using Rust). Very exciting! Does that work have significant support in the Mercurial community?

3
AlphareOPmercurial•

It is exciting, and could open up nice possibilities even outside of being a faster Mercurial (like integration of .hgignore support into ripgrep).

We definitely have a few corporate users and some people that like the bleeding-edge, but the Mercurial community as a whole (and even contributors themselves) doesn't even really use it or know about it I would say. It's hard to get the word out and we still have a ton of work to support more use-cases and platforms.

3