-
Notifications
You must be signed in to change notification settings - Fork 0
/
errs.go
146 lines (133 loc) · 3.08 KB
/
errs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Istio 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 util
import (
"fmt"
)
const (
defaultSeparator = ", "
)
// Errors is a slice of error.
type Errors []error
// Error implements the error#Error method.
func (e Errors) Error() string {
return ToString(e, defaultSeparator)
}
// String implements the stringer#String method.
func (e Errors) String() string {
return e.Error()
}
// ToError returns an error from Errors.
func (e Errors) ToError() error {
if len(e) == 0 {
return nil
}
return fmt.Errorf("%s", e)
}
// Dedup removes any duplicated errors.
func (e Errors) Dedup() Errors {
logCountMap := make(map[string]int)
for _, ee := range e {
if ee == nil {
continue
}
item := ee.Error()
_, exist := logCountMap[item]
if exist {
logCountMap[item]++
} else {
logCountMap[item] = 1
}
}
var out Errors
for _, ee := range e {
item := ee.Error()
count := logCountMap[item]
if count == 0 {
continue
}
times := ""
if count > 1 {
times = fmt.Sprintf(" (repeated %d times)", count)
}
out = AppendErr(out, fmt.Errorf("%s%s", ee, times))
// reset seen log count
logCountMap[item] = 0
}
return out
}
// NewErrs returns a slice of error with a single element err.
// If err is nil, returns nil.
func NewErrs(err error) Errors {
if err == nil {
return nil
}
return []error{err}
}
// AppendErr appends err to errors if it is not nil and returns the result.
// If err is nil, it is not appended.
func AppendErr(errors []error, err error) Errors {
if err == nil {
if len(errors) == 0 {
return nil
}
return errors
}
return append(errors, err)
}
// AppendErrs appends newErrs to errors and returns the result.
// If newErrs is empty, nothing is appended.
func AppendErrs(errors []error, newErrs []error) Errors {
if len(newErrs) == 0 {
return errors
}
for _, e := range newErrs {
errors = AppendErr(errors, e)
}
if len(errors) == 0 {
return nil
}
return errors
}
// ToString returns a string representation of errors, with elements separated by separator string. Any nil errors in the
// slice are skipped.
func ToString(errors []error, separator string) string {
var out string
for i, e := range errors {
if e == nil {
continue
}
if i != 0 {
out += separator
}
out += e.Error()
}
return out
}
// EqualErrors reports whether a and b are equal, regardless of ordering.
func EqualErrors(a, b Errors) bool {
if len(a) != len(b) {
return false
}
m := make(map[string]bool)
for _, e := range b {
m[e.Error()] = true
}
for _, ea := range a {
if !m[ea.Error()] {
return false
}
}
return true
}