Skip to content

Commit

Permalink
Merge pull request #3308 from jterry75/handle_grpc_context_error
Browse files Browse the repository at this point in the history
Add support to gRPC errdefs for context cancel/deadline exceeded
  • Loading branch information
crosbymichael committed Jun 3, 2019
2 parents 48a1fca + ac4485c commit 7451dd1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion errdefs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions errdefs/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package errdefs

import (
"context"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions errdefs/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package errdefs

import (
"context"
"testing"

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

0 comments on commit 7451dd1

Please sign in to comment.