-
Notifications
You must be signed in to change notification settings - Fork 697
Proposal/Question: Type assertions on the entire error stack #88
Comments
errors.Cause always unwraps as far as it can, ie to the bottom of the stack, or at least until you hit an error which does not implement What is it you want to do? Do you want to recover |
That's understood. It's that the stack of errors lose all type information except for the one on the top and the one at the bottom.
That is a solution but it becomes a mandate that error types with Cause methods can only have Cause methods. |
I am wondering the same thing. Reading through @davecheney article https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully the advice is to use behavior. If the behavior is somewhere in the middle of the stack and that error implements Cause, I currently don't have a way to easily get that via this pkg. I would have to implement my own unwind. |
@danilobuerger My original post has a generalized but not optimal Recurse method for unwinding. I'm still wavering on a design, though, which wraps both this issue and key-value logging concerns. |
Related proposed solution: #144 |
How would any of you propose to assert the entire wrapped error stack? Without If you want to task to each person the interface matching on each error in the cause chain… you can already do that, because you can type assert yourself in your own code. There is no way within the bounds of go lang at this time to do any thing about making this less difficult or arduous an adventure. |
Right, that's why #144 suggests making |
I think this is addressed with standard |
Originally, checking if a error matches a given interface, you would simply
m, ok := err.(myInterface)
. With pkg/errors, it's nowm, ok := errors.Cause(err).(myInterface)
(Basing this off of the writing here: http://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully)However, this falls apart if your error stack has
myInterface
in the middle, sinceerrors.Cause(err).(myInterface)
only checks the bottom of the stack:Question: What is the best way to handle this? Custom functions for each of your interfaces which may
get paired with a
causer
. A generalized function added to pkg/errors?I've thrown up some code here which shows the varying cases and solutions https://play.golang.org/p/E8eJeWbOuV
The text was updated successfully, but these errors were encountered: