Skip to content

Commit ac4485c

Browse files
committed
Add support to gRPC errdefs for context cancel/deadline exceeded
Signed-off-by: Justin Terry (VM) <[email protected]>
1 parent 0e7a3c9 commit ac4485c

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

errdefs/errors.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
// client-side errors to the correct types.
2727
package errdefs
2828

29-
import "github.com/pkg/errors"
29+
import (
30+
"context"
31+
32+
"github.com/pkg/errors"
33+
)
3034

3135
// Definitions of common error types used throughout containerd. All containerd
3236
// errors returned by most packages will map into one of these errors classes.
@@ -76,3 +80,14 @@ func IsUnavailable(err error) bool {
7680
func IsNotImplemented(err error) bool {
7781
return errors.Cause(err) == ErrNotImplemented
7882
}
83+
84+
// IsCanceled returns true if the error is due to `context.Canceled`.
85+
func IsCanceled(err error) bool {
86+
return errors.Cause(err) == context.Canceled
87+
}
88+
89+
// IsDeadlineExceeded returns true if the error is due to
90+
// `context.DeadlineExceeded`.
91+
func IsDeadlineExceeded(err error) bool {
92+
return errors.Cause(err) == context.DeadlineExceeded
93+
}

errdefs/grpc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package errdefs
1818

1919
import (
20+
"context"
2021
"strings"
2122

2223
"github.com/pkg/errors"
@@ -55,6 +56,10 @@ func ToGRPC(err error) error {
5556
return status.Errorf(codes.Unavailable, err.Error())
5657
case IsNotImplemented(err):
5758
return status.Errorf(codes.Unimplemented, err.Error())
59+
case IsCanceled(err):
60+
return status.Errorf(codes.Canceled, err.Error())
61+
case IsDeadlineExceeded(err):
62+
return status.Errorf(codes.DeadlineExceeded, err.Error())
5863
}
5964

6065
return err
@@ -89,6 +94,10 @@ func FromGRPC(err error) error {
8994
cls = ErrFailedPrecondition
9095
case codes.Unimplemented:
9196
cls = ErrNotImplemented
97+
case codes.Canceled:
98+
cls = context.Canceled
99+
case codes.DeadlineExceeded:
100+
cls = context.DeadlineExceeded
92101
default:
93102
cls = ErrUnknown
94103
}

errdefs/grpc_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package errdefs
1818

1919
import (
20+
"context"
2021
"testing"
2122

2223
"google.golang.org/grpc/codes"
@@ -56,6 +57,26 @@ func TestGRPCRoundTrip(t *testing.T) {
5657
cause: ErrUnknown,
5758
str: errShouldLeaveAlone.Error() + ": " + ErrUnknown.Error(),
5859
},
60+
{
61+
input: context.Canceled,
62+
cause: context.Canceled,
63+
str: "context canceled",
64+
},
65+
{
66+
input: errors.Wrapf(context.Canceled, "this is a test cancel"),
67+
cause: context.Canceled,
68+
str: "this is a test cancel: context canceled",
69+
},
70+
{
71+
input: context.DeadlineExceeded,
72+
cause: context.DeadlineExceeded,
73+
str: "context deadline exceeded",
74+
},
75+
{
76+
input: errors.Wrapf(context.DeadlineExceeded, "this is a test deadline exceeded"),
77+
cause: context.DeadlineExceeded,
78+
str: "this is a test deadline exceeded: context deadline exceeded",
79+
},
5980
} {
6081
t.Run(testcase.input.Error(), func(t *testing.T) {
6182
t.Logf("input: %v", testcase.input)

0 commit comments

Comments
 (0)