mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 15:39:39 +00:00
Merge pull request #100768 from Iceber/fix-nested-key-error
client-go/cache: support errors.Unwrap for KeyError Kubernetes-commit: 9b9a67ad530daeb397b1b74c0104e65b14d3ca21
This commit is contained in:
commit
d974964d12
5
tools/cache/store.go
vendored
5
tools/cache/store.go
vendored
@ -85,6 +85,11 @@ func (k KeyError) Error() string {
|
|||||||
return fmt.Sprintf("couldn't create key for object %+v: %v", k.Obj, k.Err)
|
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
|
// ExplicitKey can be passed to MetaNamespaceKeyFunc if you have the key for
|
||||||
// the object but not the object itself.
|
// the object but not the object itself.
|
||||||
type ExplicitKey string
|
type ExplicitKey string
|
||||||
|
16
tools/cache/store_test.go
vendored
16
tools/cache/store_test.go
vendored
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
@ -154,3 +155,18 @@ func TestUndeltaStore(t *testing.T) {
|
|||||||
func TestIndex(t *testing.T) {
|
func TestIndex(t *testing.T) {
|
||||||
doTestIndex(t, NewIndexer(testStoreKeyFunc, testStoreIndexers()))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user