File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626// client-side errors to the correct types.
2727package 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 {
7680func 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+ }
Original file line number Diff line number Diff line change 1717package errdefs
1818
1919import (
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 }
Original file line number Diff line number Diff line change 1717package errdefs
1818
1919import (
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 )
You can’t perform that action at this time.
0 commit comments