http-rs / http-types

Common types for HTTP operations
https://docs.rs/http-types
Apache License 2.0
200 stars 83 forks source link

refactor: overhaul errors #395

Open Fishrock123 opened 2 years ago

Fishrock123 commented 2 years ago

Overhauls errors to allow for 3 types of errors:

There is some exposition for this in Zulip: https://http-rs.zulipchat.com/#narrow/stream/261414-http-types/topic/New.20error.20types.20.26.20result.20apis/near/254273467

Basically this solves two problems:

The first problem is pretty simple to solve but was quite laborious. It meant changing all those bail!("some string error") and what not to be concrete enum variants to some degree of specificity. I have kept some errors grouped together where the string output could mean something to anyone debugging a problem but is unlikely to be something which someone would reasonably want to check during program runtime. This is to reduce the amount of labor doing this but also to cap the enum variants and prevent the number of variants from becoming unmanageable.

The second problem is tricky and weird. My solution is RequestError. This allows dynamic errors from surf middleware but also allows us to have concrete errors there. Some indirection is required for ResponseError due to StdError's conflict with anyhow (The problem is a conflict with From<T> for T, which I will never not be profusely annoyed at).


There are some potential issues here / open questions:


Edit: the diff is obviously quite large, so here are the interesting parts:

Fishrock123 commented 2 years ago

Hmph. The CI duly notes that this makes interop with hyper http more difficult (obviously).

Fishrock123 commented 2 years ago

@yoshuawuyts Hey I would like to merge this tomorrow if there is no objection

Fishrock123 commented 2 years ago

Made one additional change to this... all the enums are #[non_exhaustive] now so we can add more cases in minors where it fits well

joshtriplett commented 2 years ago

Posted a few bits of feedback, but generally this looks great. Thanks for the substantial amount of work here.

Fishrock123 commented 2 years ago

@yoshuawuyts if you are going to write a bunch of typed headers could you please take a look at this first?

yoshuawuyts commented 2 years ago

@Fishrock123 thanks so much for the ping! - we were actually running into very similar issues around error handling while looking to integrate http-types into the Azure SDK (https://github.com/Azure/azure-sdk-for-rust/issues/848), and I'm fairly comfortable saying that this seems like the right direction to go.

I still need to look at this line-by-line, but want to at least respond that I agree that we should land this before any further work on typed headers.

rbtcollins commented 2 years ago

I haven't read the diff, but just a quick though since future API breakage seems to be a strong concern here. Consider making all the concrete enums nonexhaustive. That will permit expanding them in future without API breakage - everyone will need to explicitly handle unexpected errors and not assume the error space will expand. (See e.g. filesystem errors in the stdlib for the pain of not doing this in advance).

Fishrock123 commented 2 years ago

I haven't read the diff, but just a quick though since future API breakage seems to be a strong concern here. Consider making all the concrete enums nonexhaustive. That will permit expanding them in future without API breakage - everyone will need to explicitly handle unexpected errors and not assume the error space will expand. (See e.g. filesystem errors in the stdlib for the pain of not doing this in advance).

Yes, it will be a large api change. I think that’s fine since no one really likes the current api anyway.

Also, yes I already did mark them all as #[non_exhaustive]: https://github.com/http-rs/http-types/pull/395/files#diff-87f966704ebddb378e447e13ddf9ee203cd9d31bb502338a5f3ed3478df41588R9

OneOfOne commented 5 months ago

I'm maintaining a hard fork of this repo that I plan to keep up to date, please resubmit this PR at https://github.com/OneOfOne/http-types-rs