mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 10:43:56 +00:00
deduplicate aggregated errors when generating Error() string
This commit is contained in:
parent
46fc4dd96a
commit
d7aee90f36
@ -20,6 +20,7 @@ go_library(
|
|||||||
],
|
],
|
||||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/util/errors",
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/util/errors",
|
||||||
importpath = "k8s.io/apimachinery/pkg/util/errors",
|
importpath = "k8s.io/apimachinery/pkg/util/errors",
|
||||||
|
deps = ["//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library"],
|
||||||
)
|
)
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
|
@ -19,6 +19,8 @@ package errors
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MessageCountMap contains occurrence for each error message.
|
// MessageCountMap contains occurrence for each error message.
|
||||||
@ -67,12 +69,23 @@ func (agg aggregate) Error() string {
|
|||||||
if len(agg) == 1 {
|
if len(agg) == 1 {
|
||||||
return agg[0].Error()
|
return agg[0].Error()
|
||||||
}
|
}
|
||||||
result := fmt.Sprintf("[%s", agg[0].Error())
|
seenerrs := sets.NewString()
|
||||||
for i := 1; i < len(agg); i++ {
|
result := ""
|
||||||
result += fmt.Sprintf(", %s", agg[i].Error())
|
for _, err := range agg {
|
||||||
|
msg := err.Error()
|
||||||
|
if seenerrs.Has(msg) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenerrs.Insert(msg)
|
||||||
|
if len(seenerrs) > 1 {
|
||||||
|
result += ", "
|
||||||
|
}
|
||||||
|
result += msg
|
||||||
}
|
}
|
||||||
result += "]"
|
if len(seenerrs) == 1 {
|
||||||
return result
|
return result
|
||||||
|
}
|
||||||
|
return "[" + result + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errors is part of the Aggregate interface.
|
// Errors is part of the Aggregate interface.
|
||||||
|
@ -147,6 +147,38 @@ func TestPluralAggregate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDedupeAggregate(t *testing.T) {
|
||||||
|
var slice []error = []error{fmt.Errorf("abc"), fmt.Errorf("abc")}
|
||||||
|
var agg Aggregate
|
||||||
|
|
||||||
|
agg = NewAggregate(slice)
|
||||||
|
if agg == nil {
|
||||||
|
t.Errorf("expected non-nil")
|
||||||
|
}
|
||||||
|
if s := agg.Error(); s != "abc" {
|
||||||
|
t.Errorf("expected 'abc', got %q", s)
|
||||||
|
}
|
||||||
|
if s := agg.Errors(); len(s) != 2 {
|
||||||
|
t.Errorf("expected two-elements slice, got %#v", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDedupePluralAggregate(t *testing.T) {
|
||||||
|
var slice []error = []error{fmt.Errorf("abc"), fmt.Errorf("abc"), fmt.Errorf("123")}
|
||||||
|
var agg Aggregate
|
||||||
|
|
||||||
|
agg = NewAggregate(slice)
|
||||||
|
if agg == nil {
|
||||||
|
t.Errorf("expected non-nil")
|
||||||
|
}
|
||||||
|
if s := agg.Error(); s != "[abc, 123]" {
|
||||||
|
t.Errorf("expected '[abc, 123]', got %q", s)
|
||||||
|
}
|
||||||
|
if s := agg.Errors(); len(s) != 3 {
|
||||||
|
t.Errorf("expected three-elements slice, got %#v", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFilterOut(t *testing.T) {
|
func TestFilterOut(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
err error
|
err error
|
||||||
|
Loading…
Reference in New Issue
Block a user