-
Notifications
You must be signed in to change notification settings - Fork 111
Forward Display implementation for foreign errors #13
Forward Display implementation for foreign errors #13
Conversation
Oh thanks! Gosh, I didn't fully realize the implications of this issue. This looks to me like foreign errors must be Instead of adding a field to the
You could imagine this function containing a match statement on It may also make sense to store foreign errors in the Let me know what you think. |
Yeah, this PR as it stands requires the errors are copy-- I suppose that was more than a little presumptuous of me. I'll take a look at the solutions you suggested and let you know what I find out. |
I'm now including the Display of all errors with a cause. The output format is ": " if it has an error, and "" otherwise. Do you think that's okay behavior, or would you prefer we specifically tag foreign error variants and only print the cause for those types? |
@cramertj Foreign errors are a bit of a special case: whereas error-chain ErrorKinds represent an 'actual' error, foreign error ErrorKinds are more of a stand-in for the boxed foreign error (the ErrorKind description is more-or-less useless - as demonstrated by this bug). So I think I prefer for display to delegate to the cause only when its a foreign error. But that raises another question: if we are printing the boxed cause as the error, then what happens when you iterate over the entire chain of causes to print them each during error reporting? We're going to see duplicates. One might consider having the Another approach we could possibly take is to do what you were originally doing - storing the foreign error in the ErrorKind, but storing Tradeoffs abound. Do those considerations make sense? Not sure how I feel. What are your thoughts? |
Out of those two options, I'd prefer the latter as it seems more consistent with the design of the overall library. The former seems like it's trying to bypass the error<->cause relationship. On a more intuitive level, the second representation (with the error stored in the ErrorKind with None in the cause) makes more sense to me: the foreign error ErrorKind variant isn't really caused by the foreign error, it's a wrapped representation of the foreign error. Because it's just a wrapper, it makes sense for the foreign error ErrorKind variant to not have its own cause, but instead forward on to the cause of the type it wraps. |
@cramertj If we go that route we'll need to modify the I'm happy to try this out. Want to modify the PR to see if it works? |
Sure! I'm at work at the moment, so it'll have to wait until later this evening. Thanks for your guidance! |
a1f69e7
to
bed8310
Compare
Alright, I gave it a shot and added some tests. Let me know what you think. |
Thanks @cramertj. I've got some fires to attend to today. May not get back to this until tomorrow. |
Oof, sorry that 'tomorrow' turned into 5 tomorrows. This look great @cramertj! I'll get a new build out today. |
Does this mean that the Display implementation will show the full chain of "native" error-chain errors, and stop at the first "foreign" error? Or will it continue through "foreign" errors as long as the .cause chain continues? |
@joshtriplett The Display implementation only shows one 'level' of errors (before and after this patch), but the |
@joshtriplett In that example |
In other words, compared to the previous implementation the "git error" 'level' doesn't exist at all. This implementation removes a (useless) link in the chain. |
Although, you might (and I kinda did) consider that extra level useful in that it is in some sense a delimiter between the native and foreign errors. If that matters, we might consider adding a method to e.g. indicate whether an error is a foreign error or not. |
@brson Actually, depending on how much you care about backwards-compatibility, it might be a good idea to remove the "description" option from the foreign link variant and make the description forward to the description of the underlying error. Right now, I think the underlying error description is unaccessible. |
Fix #2.
One caveat to this is that it adds the type of the foreign error to the signature of the error-chain foreign_link_variant. As a result, the visibility of the foreign error type must be the same as the visibility of the error-chain type (public).