mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
api: wrap underlying EtcdError error
Without wrapping, using `errors.Is()` (or even trying to compare the error with `==`) fails as EtcdError swallows the underlying error. Signed-off-by: Ivan Valdes <ivan@vald.es>
This commit is contained in:
parent
fdbde77a92
commit
c837473a0f
@ -239,8 +239,9 @@ var (
|
||||
// EtcdError defines gRPC server errors.
|
||||
// (https://github.com/grpc/grpc-go/blob/master/rpc_util.go#L319-L323)
|
||||
type EtcdError struct {
|
||||
code codes.Code
|
||||
desc string
|
||||
code codes.Code
|
||||
desc string
|
||||
wrappedError error
|
||||
}
|
||||
|
||||
// Code returns grpc/codes.Code.
|
||||
@ -253,6 +254,10 @@ func (e EtcdError) Error() string {
|
||||
return e.desc
|
||||
}
|
||||
|
||||
func (e EtcdError) Unwrap() error {
|
||||
return e.wrappedError
|
||||
}
|
||||
|
||||
func Error(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
@ -268,7 +273,7 @@ func Error(err error) error {
|
||||
} else {
|
||||
desc = verr.Error()
|
||||
}
|
||||
return EtcdError{code: ev.Code(), desc: desc}
|
||||
return EtcdError{code: ev.Code(), desc: desc, wrappedError: err}
|
||||
}
|
||||
|
||||
func ErrorDesc(err error) string {
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
package rpctypes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -40,3 +41,20 @@ func TestConvert(t *testing.T) {
|
||||
t.Fatalf("expected them to be equal, got %v / %v", ev2.Code(), e3.(EtcdError).Code())
|
||||
}
|
||||
}
|
||||
|
||||
func TestComparingWrappedError(t *testing.T) {
|
||||
errTest := errors.New("test error")
|
||||
e1 := Error(ErrGRPCEmptyKey)
|
||||
e2 := Error(status.Error(codes.InvalidArgument, "etcdserver: key is not provided"))
|
||||
e3 := Error(errTest)
|
||||
|
||||
if !errors.Is(e1, ErrGRPCEmptyKey) {
|
||||
t.Fatalf("expected %v to be an ErrGRPCEmptyKey wrapped error", e1)
|
||||
}
|
||||
if !errors.Is(e2, ErrGRPCEmptyKey) {
|
||||
t.Fatalf("expected %v to be an ErrGRPCEmptyKey wrapped error", e1)
|
||||
}
|
||||
if !errors.Is(e3, errTest) {
|
||||
t.Fatalf("expected %v to be an errTest wrapped error", e3)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user