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`.

Kubernetes-commit: 6b7c365f8f6d50280c2dab171efdd4b93d964f32
This commit is contained in:
Arda Güçlü 2022-12-19 15:26:08 +03:00 committed by Kubernetes Publisher
parent 19c9966172
commit 21fffc1153
2 changed files with 3 additions and 2 deletions

View File

@ -136,7 +136,7 @@ func UntilWithSync(ctx context.Context, lw cache.ListerWatcher, objType runtime.
if precondition != nil { if precondition != nil {
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) { 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) done, err := precondition(indexer)

View File

@ -19,6 +19,7 @@ package watch
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -227,7 +228,7 @@ func TestUntilWithSync(t *testing.T) {
conditionFunc: func(e watch.Event) (bool, error) { conditionFunc: func(e watch.Event) (bool, error) {
return true, nil 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, expectedEvent: nil,
}, },
{ {