Skip to content
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

api: wrap underlying EtcdError error #18578

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions api/v3rpc/rpctypes/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions api/v3rpc/rpctypes/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package rpctypes

import (
"errors"
"testing"

"google.golang.org/grpc/codes"
Expand All @@ -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