From 6b7c365f8f6d50280c2dab171efdd4b93d964f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Mon, 19 Dec 2022 15:26:08 +0300 Subject: [PATCH] Embed context deadline exceeded in error to let errors.Is can work Currently, watch package embeds context deadlineexceeded error in it's own error using `%v`, as can be seen in here; `fmt.Errorf("UntilWithSync: unable to sync caches: %v", ctx.Err())` However, consumers of this function can not use `errors.Is(err, context.DeadlineExceeded)` due this `%v`. To let consumers can distinguish context deadlineexceeded errors, this PR changes error embedding format to `%w`. --- staging/src/k8s.io/client-go/tools/watch/until.go | 2 +- staging/src/k8s.io/client-go/tools/watch/until_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/client-go/tools/watch/until.go b/staging/src/k8s.io/client-go/tools/watch/until.go index 27080c16cd2..a2474556b0a 100644 --- a/staging/src/k8s.io/client-go/tools/watch/until.go +++ b/staging/src/k8s.io/client-go/tools/watch/until.go @@ -136,7 +136,7 @@ func UntilWithSync(ctx context.Context, lw cache.ListerWatcher, objType runtime. if precondition != nil { if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) { - return nil, fmt.Errorf("UntilWithSync: unable to sync caches: %v", ctx.Err()) + return nil, fmt.Errorf("UntilWithSync: unable to sync caches: %w", ctx.Err()) } done, err := precondition(indexer) diff --git a/staging/src/k8s.io/client-go/tools/watch/until_test.go b/staging/src/k8s.io/client-go/tools/watch/until_test.go index e8ab6cab4f8..2d64c423ac2 100644 --- a/staging/src/k8s.io/client-go/tools/watch/until_test.go +++ b/staging/src/k8s.io/client-go/tools/watch/until_test.go @@ -19,6 +19,7 @@ package watch import ( "context" "errors" + "fmt" "reflect" "strings" "testing" @@ -227,7 +228,7 @@ func TestUntilWithSync(t *testing.T) { conditionFunc: func(e watch.Event) (bool, error) { return true, nil }, - expectedErr: errors.New("UntilWithSync: unable to sync caches: context deadline exceeded"), + expectedErr: fmt.Errorf("UntilWithSync: unable to sync caches: %w", context.DeadlineExceeded), expectedEvent: nil, }, {