summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/util/errors/errors.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/util/errors/errors.go')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/util/errors/errors.go32
1 files changed, 26 insertions, 6 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/util/errors/errors.go b/vendor/k8s.io/apimachinery/pkg/util/errors/errors.go
index 62a73f34e..5bafc218e 100644
--- a/vendor/k8s.io/apimachinery/pkg/util/errors/errors.go
+++ b/vendor/k8s.io/apimachinery/pkg/util/errors/errors.go
@@ -28,9 +28,14 @@ type MessageCountMap map[string]int
// Aggregate represents an object that contains multiple errors, but does not
// necessarily have singular semantic meaning.
+// The aggregate can be used with `errors.Is()` to check for the occurrence of
+// a specific error type.
+// Errors.As() is not supported, because the caller presumably cares about a
+// specific error of potentially multiple that match the given type.
type Aggregate interface {
error
Errors() []error
+ Is(error) bool
}
// NewAggregate converts a slice of errors into an Aggregate interface, which
@@ -71,16 +76,17 @@ func (agg aggregate) Error() string {
}
seenerrs := sets.NewString()
result := ""
- agg.visit(func(err error) {
+ agg.visit(func(err error) bool {
msg := err.Error()
if seenerrs.Has(msg) {
- return
+ return false
}
seenerrs.Insert(msg)
if len(seenerrs) > 1 {
result += ", "
}
result += msg
+ return false
})
if len(seenerrs) == 1 {
return result
@@ -88,19 +94,33 @@ func (agg aggregate) Error() string {
return "[" + result + "]"
}
-func (agg aggregate) visit(f func(err error)) {
+func (agg aggregate) Is(target error) bool {
+ return agg.visit(func(err error) bool {
+ return errors.Is(err, target)
+ })
+}
+
+func (agg aggregate) visit(f func(err error) bool) bool {
for _, err := range agg {
switch err := err.(type) {
case aggregate:
- err.visit(f)
+ if match := err.visit(f); match {
+ return match
+ }
case Aggregate:
for _, nestedErr := range err.Errors() {
- f(nestedErr)
+ if match := f(nestedErr); match {
+ return match
+ }
}
default:
- f(err)
+ if match := f(err); match {
+ return match
+ }
}
}
+
+ return false
}
// Errors is part of the Aggregate interface.