Add support to gRPC errdefs for context cancel/deadline exceeded

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM) 2019-05-28 17:02:29 -07:00
parent 0e7a3c9e51
commit ac4485c76a
3 changed files with 46 additions and 1 deletions

View File

@ -26,7 +26,11 @@
// client-side errors to the correct types. // client-side errors to the correct types.
package errdefs package errdefs
import "github.com/pkg/errors" import (
"context"
"github.com/pkg/errors"
)
// Definitions of common error types used throughout containerd. All containerd // Definitions of common error types used throughout containerd. All containerd
// errors returned by most packages will map into one of these errors classes. // 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 { func IsNotImplemented(err error) bool {
return errors.Cause(err) == ErrNotImplemented 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
}

View File

@ -17,6 +17,7 @@
package errdefs package errdefs
import ( import (
"context"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -55,6 +56,10 @@ func ToGRPC(err error) error {
return status.Errorf(codes.Unavailable, err.Error()) return status.Errorf(codes.Unavailable, err.Error())
case IsNotImplemented(err): case IsNotImplemented(err):
return status.Errorf(codes.Unimplemented, err.Error()) 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 return err
@ -89,6 +94,10 @@ func FromGRPC(err error) error {
cls = ErrFailedPrecondition cls = ErrFailedPrecondition
case codes.Unimplemented: case codes.Unimplemented:
cls = ErrNotImplemented cls = ErrNotImplemented
case codes.Canceled:
cls = context.Canceled
case codes.DeadlineExceeded:
cls = context.DeadlineExceeded
default: default:
cls = ErrUnknown cls = ErrUnknown
} }

View File

@ -17,6 +17,7 @@
package errdefs package errdefs
import ( import (
"context"
"testing" "testing"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@ -56,6 +57,26 @@ func TestGRPCRoundTrip(t *testing.T) {
cause: ErrUnknown, cause: ErrUnknown,
str: errShouldLeaveAlone.Error() + ": " + ErrUnknown.Error(), 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.Run(testcase.input.Error(), func(t *testing.T) {
t.Logf("input: %v", testcase.input) t.Logf("input: %v", testcase.input)