mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Apimachinery meta errors: Support errors.Is and error wrapping
Currently, the errors in the pkg/api/meta package don't work correctly with the stdlibs `errors.Is` because they do not implement an `Is` method, which makes the matching fall through to use reflect to check for equality. This change fixes that and as a side-effect also adds support to match on wrapped errors.
This commit is contained in:
parent
7b1b801295
commit
b285beeb3e
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package meta
|
package meta
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
@ -43,6 +44,11 @@ func (e *AmbiguousResourceError) Error() string {
|
|||||||
return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource)
|
return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*AmbiguousResourceError) Is(target error) bool {
|
||||||
|
_, ok := target.(*AmbiguousResourceError)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind
|
// AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind
|
||||||
type AmbiguousKindError struct {
|
type AmbiguousKindError struct {
|
||||||
PartialKind schema.GroupVersionKind
|
PartialKind schema.GroupVersionKind
|
||||||
@ -63,16 +69,16 @@ func (e *AmbiguousKindError) Error() string {
|
|||||||
return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind)
|
return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*AmbiguousKindError) Is(target error) bool {
|
||||||
|
_, ok := target.(*AmbiguousKindError)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func IsAmbiguousError(err error) bool {
|
func IsAmbiguousError(err error) bool {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
switch err.(type) {
|
return errors.Is(err, &AmbiguousResourceError{}) || errors.Is(err, &AmbiguousKindError{})
|
||||||
case *AmbiguousResourceError, *AmbiguousKindError:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoResourceMatchError is returned if the RESTMapper can't find any match for a resource
|
// NoResourceMatchError is returned if the RESTMapper can't find any match for a resource
|
||||||
@ -84,6 +90,11 @@ func (e *NoResourceMatchError) Error() string {
|
|||||||
return fmt.Sprintf("no matches for %v", e.PartialResource)
|
return fmt.Sprintf("no matches for %v", e.PartialResource)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*NoResourceMatchError) Is(target error) bool {
|
||||||
|
_, ok := target.(*NoResourceMatchError)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// NoKindMatchError is returned if the RESTMapper can't find any match for a kind
|
// NoKindMatchError is returned if the RESTMapper can't find any match for a kind
|
||||||
type NoKindMatchError struct {
|
type NoKindMatchError struct {
|
||||||
// GroupKind is the API group and kind that was searched
|
// GroupKind is the API group and kind that was searched
|
||||||
@ -108,14 +119,14 @@ func (e *NoKindMatchError) Error() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*NoKindMatchError) Is(target error) bool {
|
||||||
|
_, ok := target.(*NoKindMatchError)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func IsNoMatchError(err error) bool {
|
func IsNoMatchError(err error) bool {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
switch err.(type) {
|
return errors.Is(err, &NoResourceMatchError{}) || errors.Is(err, &NoKindMatchError{})
|
||||||
case *NoResourceMatchError, *NoKindMatchError:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
79
staging/src/k8s.io/apimachinery/pkg/api/meta/errors_test.go
Normal file
79
staging/src/k8s.io/apimachinery/pkg/api/meta/errors_test.go
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package meta
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestErrorMatching(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
// input should contain an error that is _not_ empty, otherwise the naive reflectlite.DeepEqual matching of
|
||||||
|
// the errors lib will always succeed, but for all of these we want to verify that the matching is based on
|
||||||
|
// type.
|
||||||
|
input error
|
||||||
|
new func() error
|
||||||
|
matcherFunc func(error) bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "AmbiguousResourceError",
|
||||||
|
input: &AmbiguousResourceError{MatchingResources: []schema.GroupVersionResource{{}}},
|
||||||
|
new: func() error { return &AmbiguousResourceError{} },
|
||||||
|
matcherFunc: IsAmbiguousError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "AmbiguousKindError",
|
||||||
|
input: &AmbiguousKindError{MatchingResources: []schema.GroupVersionResource{{}}},
|
||||||
|
new: func() error { return &AmbiguousKindError{} },
|
||||||
|
matcherFunc: IsAmbiguousError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "NoResourceMatchError",
|
||||||
|
input: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "foo"}},
|
||||||
|
new: func() error { return &NoResourceMatchError{} },
|
||||||
|
matcherFunc: IsNoMatchError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "NoKindMatchError",
|
||||||
|
input: &NoKindMatchError{SearchedVersions: []string{"foo"}},
|
||||||
|
new: func() error { return &NoKindMatchError{} },
|
||||||
|
matcherFunc: IsNoMatchError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if !errors.Is(tc.input, tc.new()) {
|
||||||
|
t.Error("error doesn't match itself directly")
|
||||||
|
}
|
||||||
|
if !errors.Is(fmt.Errorf("wrapepd: %w", tc.input), tc.new()) {
|
||||||
|
t.Error("error doesn't match itself when wrapped")
|
||||||
|
}
|
||||||
|
if !tc.matcherFunc(tc.input) {
|
||||||
|
t.Errorf("error doesn't get matched by matcherfunc")
|
||||||
|
}
|
||||||
|
if errors.Is(tc.input, errors.New("foo")) {
|
||||||
|
t.Error("error incorrectly matches other error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user