-
-
Notifications
You must be signed in to change notification settings - Fork 330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lack of consistent error type #670
Comments
Thanks a lot for letting me know! The problem described here isn't necessarily specific to About 1: various
|
Hmm, I can see how one big sum type erases information about what errors can actually happen for a specific method. However my desire to preserve the error info is so that users can detect and respond in the usually very rare case that they care to do something different based on how the failure happened. I think the more common case is just caring whether the operation succeeded at all. So while yes information is erased by using a sum type, the common case is for users to not care. To summarize:
My assumption is that even in the rare situations where you want to vary behavior based on the type of error you probably only can do something useful in a subset of the possible errors, even when the error type already narrows the possibilities down to the failures actually specifically possible for that method. Also the sum type gives you some encapsulation / implementation freedom -- you can change which errors a method produces without breaking API, as long as it's not a completely new variant. I think if a user really wants to handle Re: a common trait, even if thiserror won't let me sprinkle an annotation to automatically generate the From impl for me, it's still easier for me to write 1 From impl myself that boxes and know I've covered the whole git-repository crate than try to keep up with a manual list. If there were a common trait I assume I could do still do this?:
|
For the time being I decided it was good enough for my purposes to just always do |
Even though the word 'terrible' isn't well suited to make an argument, it definitely describes the feelings I had when trying to use the early As Regarding the example with
Can you validate that this works? Because if so, I'd love to provide such a trait to allow library crates to do such conversion. That would be so much better than converting to By the way, I totally agree that many crates would't match on specific variants of an error, it's a rather rare event. But somehow it's not rare enough for me to not encounter it regularly, and when I do I am happy the set of possible variants is limited to only what's actually possible, with each variant yielding the possibility to describe the exact context clearly. What |
I haven't tried the common trait technique yet but wanted to document something related here while I remember. https://docs.rs/git-repository/latest/git_repository/object/tree/diff/struct.Platform.html#method.for_each_to_obtain_tree
|
I think so far no It's great to call out this inconsistency, as I also would prefer to either fully embrace generics in error types (in What do you think? |
I have no issue with error type erasure, but for places like for_each_to_obtain_tree where we have a user error generic type returned from a callback, can we change the bound from E: std::error::Error + Sync + Send + 'static to E: Into<Box<dyn std::error::Error + Sync + Send + 'static>> ? This way, more error types are possible, including |
…` errors are more convenient to use. (#670) Due to a change in how the generic error type is declared it should now be possible to use `anyhow` with it as well.
That's a great idea, I didn't think of that! The change is happening in this PR. |
Summary 💡
I'm trying to use various methods in git_repository. I usually use
thiserror
to build anError
type for my crate using#[from]
to be able to losslessly wrap every error my crate encounters, this is a pretty common idiom so I'm confident you've encountered it. When another crate I use uses the same pattern, this is pretty easy, I just extend myError
enum with the 1 new case for that crate:And then I can just stick
?
on any fallible operation and the conversion into my crate'sError
type magically happens. I'm running into 2 problems trying to usegit-repository
this way:git-repository
are generic, e.g.git_repository::git_odb::find::existing::Error<git_repository::git_odb::store::find::Error>
so there's not even a finite number of them, further exploding the number of variants I need to add.I wouldn't say
git-repository
is doing anything "wrong", which is why this is a feature request. I don't know if there's a good motivation for doing errors this way, it just makes it more difficult to apply the idiom that I've learned for lossless error propagation.If I could write one
impl
that would let me box any error coming fromgit-repository
that would also work nicely, but as is now I think the only trait they have in common is thestd::error::Error
trait, so AFAIK there is no good way to only do this forgit-repository
errors automatically.Motivation 🔦
Lossless error handling, generally avoiding boxing where I can. I could switch to something like
anyhow
, but I'm writing a library crate so I prefer to keep user's options open.The text was updated successfully, but these errors were encountered: