Add support to gRPC errdefs for context cancel/deadline exceeded
Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
parent
0e7a3c9e51
commit
ac4485c76a
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user