From ac4485c76af74a538406d454e13200dba573eb3f Mon Sep 17 00:00:00 2001 From: "Justin Terry (VM)" Date: Tue, 28 May 2019 17:02:29 -0700 Subject: [PATCH] Add support to gRPC errdefs for context cancel/deadline exceeded Signed-off-by: Justin Terry (VM) --- errdefs/errors.go | 17 ++++++++++++++++- errdefs/grpc.go | 9 +++++++++ errdefs/grpc_test.go | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/errdefs/errors.go b/errdefs/errors.go index 40427fc5a..b5200afc0 100644 --- a/errdefs/errors.go +++ b/errdefs/errors.go @@ -26,7 +26,11 @@ // client-side errors to the correct types. package errdefs -import "github.com/pkg/errors" +import ( + "context" + + "github.com/pkg/errors" +) // Definitions of common error types used throughout containerd. All containerd // errors returned by most packages will map into one of these errors classes. @@ -76,3 +80,14 @@ func IsUnavailable(err error) bool { func IsNotImplemented(err error) bool { return errors.Cause(err) == ErrNotImplemented } + +// IsCanceled returns true if the error is due to `context.Canceled`. +func IsCanceled(err error) bool { + return errors.Cause(err) == context.Canceled +} + +// IsDeadlineExceeded returns true if the error is due to +// `context.DeadlineExceeded`. +func IsDeadlineExceeded(err error) bool { + return errors.Cause(err) == context.DeadlineExceeded +} diff --git a/errdefs/grpc.go b/errdefs/grpc.go index b1542f13d..209f63bd0 100644 --- a/errdefs/grpc.go +++ b/errdefs/grpc.go @@ -17,6 +17,7 @@ package errdefs import ( + "context" "strings" "github.com/pkg/errors" @@ -55,6 +56,10 @@ func ToGRPC(err error) error { return status.Errorf(codes.Unavailable, err.Error()) case IsNotImplemented(err): return status.Errorf(codes.Unimplemented, err.Error()) + case IsCanceled(err): + return status.Errorf(codes.Canceled, err.Error()) + case IsDeadlineExceeded(err): + return status.Errorf(codes.DeadlineExceeded, err.Error()) } return err @@ -89,6 +94,10 @@ func FromGRPC(err error) error { cls = ErrFailedPrecondition case codes.Unimplemented: cls = ErrNotImplemented + case codes.Canceled: + cls = context.Canceled + case codes.DeadlineExceeded: + cls = context.DeadlineExceeded default: cls = ErrUnknown } diff --git a/errdefs/grpc_test.go b/errdefs/grpc_test.go index a74e53b4a..31143dbc1 100644 --- a/errdefs/grpc_test.go +++ b/errdefs/grpc_test.go @@ -17,6 +17,7 @@ package errdefs import ( + "context" "testing" "google.golang.org/grpc/codes" @@ -56,6 +57,26 @@ func TestGRPCRoundTrip(t *testing.T) { cause: ErrUnknown, str: errShouldLeaveAlone.Error() + ": " + ErrUnknown.Error(), }, + { + input: context.Canceled, + cause: context.Canceled, + str: "context canceled", + }, + { + input: errors.Wrapf(context.Canceled, "this is a test cancel"), + cause: context.Canceled, + str: "this is a test cancel: context canceled", + }, + { + input: context.DeadlineExceeded, + cause: context.DeadlineExceeded, + str: "context deadline exceeded", + }, + { + input: errors.Wrapf(context.DeadlineExceeded, "this is a test deadline exceeded"), + cause: context.DeadlineExceeded, + str: "this is a test deadline exceeded: context deadline exceeded", + }, } { t.Run(testcase.input.Error(), func(t *testing.T) { t.Logf("input: %v", testcase.input)