diff --git a/tools/cache/store.go b/tools/cache/store.go index 886e95d2..24ffabab 100644 --- a/tools/cache/store.go +++ b/tools/cache/store.go @@ -85,6 +85,11 @@ func (k KeyError) Error() string { return fmt.Sprintf("couldn't create key for object %+v: %v", k.Obj, k.Err) } +// Unwrap implements errors.Unwrap +func (k KeyError) Unwrap() error { + return k.Err +} + // ExplicitKey can be passed to MetaNamespaceKeyFunc if you have the key for // the object but not the object itself. type ExplicitKey string diff --git a/tools/cache/store_test.go b/tools/cache/store_test.go index 52c9585a..232211ee 100644 --- a/tools/cache/store_test.go +++ b/tools/cache/store_test.go @@ -17,6 +17,7 @@ limitations under the License. package cache import ( + "errors" "testing" "k8s.io/apimachinery/pkg/util/sets" @@ -154,3 +155,18 @@ func TestUndeltaStore(t *testing.T) { func TestIndex(t *testing.T) { doTestIndex(t, NewIndexer(testStoreKeyFunc, testStoreIndexers())) } + +func TestKeyError(t *testing.T) { + obj := 100 + err := errors.New("error") + keyErr := KeyError{obj, err} + + if errors.Unwrap(keyErr) != err { + t.Errorf("expected unwrap error: %v", err) + } + + nestedKeyErr := KeyError{obj, keyErr} + if !errors.Is(keyErr, err) || !errors.Is(nestedKeyErr, err) { + t.Errorf("not match target error: %v", err) + } +}