This repository was archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 707
Proposal: errors.Pop() #84
Comments
Thank you for this suggestion. I understand what you want to implement, but I do not understand why. Can you show me how a caller of |
Hi! var (
ErrNetwork = errors.New("network erorr")
ErrDB = errors.New("database erorr")
)
func main() {
err := funcWithError()
switch errors.Pop(err) {
case ErrNetwork:
// do stuff
case ErrDB:
// do stuff
default:
// do other stuff
}
...
} |
@tux-eithel thanks for your comment. I don't think your sample code works because the errors returned from this package cannot be compared for equality. This is by design. |
@davecheney My example it too much simplified. The var (
ErrNetwork = errors.New("network erorr")
ErrDB = errors.New("database erorr")
)
func main() {
err := funcWithError()
pop := errors.Pop(err)
switch pop.Error() {
case ErrNetwork:
// do stuff
case ErrDB:
// do stuff
default:
// some error from other libraries
}
...
} |
You can write if err, ok := err.(interface{Cause() error}); ok {
cause := err.Cause()
///
} |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hello, we are wrapping our errors in our grpc service, and we came across a situation where we wanted to return the top of the wrapped errors in the return of our endpoints.
For reference, a typical grpc endpoint signature looks like this:
The method signature has an
error
type as the second return value. We've written grpc middleware that receives the return values of this function, and does the proper logging, metrics, etc, before ultimately returning to the requesting client.In the middleware, we log the entire error chain, but what we'd like to return to the grpc client in the
error
return param is the top error in the chain. So I'm proposing the ability to do this:Currently there's no easy (that I know of 😄 ) way to get the top most of an error chain. This would allow us to do so.
Thoughts?
The text was updated successfully, but these errors were encountered: