-
Notifications
You must be signed in to change notification settings - Fork 80
/
list.go
203 lines (187 loc) · 4.81 KB
/
list.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022 Mineiros GmbH
//
// 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 errors
import (
"errors"
"fmt"
"strings"
"github.com/hashicorp/hcl/v2"
)
// List represents a list of error instances that also
// implements Go's error interface.
//
// List implements Go's errors.Is protocol matching the
// target error with all the errors inside it, returning
// true if any of the errors is a match.
type List struct {
errs []error
}
// L builds a List instance with all errs provided as arguments.
// Any nil errors on errs will be discarded.
//
// Any error of type hcl.Diagnostics will be flattened inside
// the error list, each hcl.Diagnostic will become an error.Error.
//
// Any error of type errors.List will be flattened inside
// the error list, each will become an error.Error.
func L(errs ...error) *List {
e := &List{}
for _, err := range errs {
e.Append(err)
}
return e
}
// Error returns the string representation of the error list.
// Only the first error message is returned, all other errors are elided.
// For a full representation of all errors use the List.Detailed method.
func (l *List) Error() string {
if len(l.errs) == 0 {
return ""
}
errmsg := l.errs[0].Error()
if len(l.errs) == 1 {
return errmsg
}
return fmt.Sprintf("%s (and %d elided errors)", errmsg, len(l.errs)-1)
}
// Errors returns all errors contained on the list.
// It flattens out the wrapped error lists inside *error.Error.
func (l *List) Errors() []error {
var errs []error
for _, err := range l.errs {
var (
e *Error
el *List
)
if errors.As(err, &el) {
errs = append(errs, el.Errors()...)
} else if errors.As(err, &e) {
errs = append(errs, e)
} else {
errs = append(errs, err)
}
}
return errs
}
// Detailed returns a detailed string representation of the error list.
// It will return all errors contained on the list as a single string.
// One error per line.
func (l *List) Detailed() string {
if len(l.errs) == 0 {
return ""
}
details := []string{"error list:"}
for _, err := range l.errs {
var errmsg string
if e, ok := err.(interface{ Detailed() string }); ok {
errmsg = e.Detailed()
} else {
errmsg = err.Error()
}
details = append(details, "\t-"+errmsg)
}
return strings.Join(details, "\n")
}
// Append appends the provided errs on the error list, ignoring nil values.
//
// Any error of type hcl.Diagnostics will have its hcl.Diagnostic elements added
// to the error list.
//
// Any error of type errors.List will be flattened inside
// the error list.
func (l *List) Append(errs ...error) {
if len(errs) == 0 {
return
}
for _, err := range errs {
if err == nil {
continue
}
switch e := err.(type) {
case hcl.Diagnostics:
if e != nil {
for _, diag := range e.Errs() {
l.Append(E(diag))
}
}
case *List:
if e != nil {
for _, err := range e.errs {
l.Append(err)
}
}
case *Error:
if e != nil {
if el, ok := e.Err.(*List); ok {
l.errs = append(l.errs, el.errs...)
} else {
l.errs = append(l.errs, e)
}
}
case *hcl.Diagnostic:
if e != nil {
l.errs = append(l.errs, err)
}
default:
l.errs = append(l.errs, err)
}
}
}
// AppendWrap is like Append() but wrap all errors with the provided kind.
func (l *List) AppendWrap(kind Kind, errs ...error) {
for _, err := range errs {
if err != nil {
l.Append(E(kind, err))
}
}
}
// AsError returns the error list as an error instance if the errors
// list is non-empty.
// If the list is empty it will return nil.
func (l *List) AsError() error {
if len(l.errs) == 0 {
return nil
}
return l
}
func (l *List) len() int { return len(l.errs) }
// Is will call errors.Is for each of the errors on its list
// returning true on the first match it finds or false if no
// error inside the list matches the given target.
//
// If target is also an *error.List then the target list must have the same
// errors inside on the same order.
func (l *List) Is(target error) bool {
if targetList, ok := target.(*List); ok {
return equalLists(l, targetList)
}
for _, err := range l.errs {
if errors.Is(err, target) {
return true
}
}
return false
}
func equalLists(l, o *List) bool {
if len(l.errs) != len(o.errs) {
return false
}
for i, lerr := range l.errs {
oerr := o.errs[i]
if !errors.Is(lerr, oerr) {
return false
}
}
return true
}