Browse code

bump hashicorp/go-multierror v1.0.0, add errwrap v1.0.0

full diff: https://github.com/hashicorp/go-multierror/compare/fcdddc395df1ddf4247c69bd436e84cfa0733f7e...v1.0.0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 720b66ee1fc8bddb32b45dbd22fb4b8359c09cf5)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/09/03 18:52:45
Showing 13 changed files
... ...
@@ -46,8 +46,9 @@ github.com/armon/go-metrics                         eb0af217e5e9747e41dd53037553
46 46
 github.com/hashicorp/go-msgpack                     71c2886f5a673a35f909803f38ece5810165097b
47 47
 github.com/hashicorp/memberlist                     3d8438da9589e7b608a83ffac1ef8211486bcb7c
48 48
 github.com/sean-/seed                               e2103e2c35297fb7e17febb81e49b312087a2372
49
+github.com/hashicorp/errwrap                        8a6fb523712970c966eefc6b39ed2c5e74880354 # v1.0.0
49 50
 github.com/hashicorp/go-sockaddr                    6d291a969b86c4b633730bfc6b8b9d64c3aafed9
50
-github.com/hashicorp/go-multierror                  fcdddc395df1ddf4247c69bd436e84cfa0733f7e
51
+github.com/hashicorp/go-multierror                  886a7fbe3eb1c874d46f623bfa70af45f425b3d1 # v1.0.0
51 52
 github.com/hashicorp/serf                           598c54895cc5a7b1a24a398d635e8c0ea0959870
52 53
 github.com/docker/libkv                             458977154600b9f23984d9f4b82e79570b5ae12b
53 54
 github.com/vishvananda/netns                        7109fa855b0ff1ebef7fbd2f6aa613e8db7cfbc0
54 55
new file mode 100644
... ...
@@ -0,0 +1,354 @@
0
+Mozilla Public License, version 2.0
1
+
2
+1. Definitions
3
+
4
+1.1. “Contributor”
5
+
6
+     means each individual or legal entity that creates, contributes to the
7
+     creation of, or owns Covered Software.
8
+
9
+1.2. “Contributor Version”
10
+
11
+     means the combination of the Contributions of others (if any) used by a
12
+     Contributor and that particular Contributor’s Contribution.
13
+
14
+1.3. “Contribution”
15
+
16
+     means Covered Software of a particular Contributor.
17
+
18
+1.4. “Covered Software”
19
+
20
+     means Source Code Form to which the initial Contributor has attached the
21
+     notice in Exhibit A, the Executable Form of such Source Code Form, and
22
+     Modifications of such Source Code Form, in each case including portions
23
+     thereof.
24
+
25
+1.5. “Incompatible With Secondary Licenses”
26
+     means
27
+
28
+     a. that the initial Contributor has attached the notice described in
29
+        Exhibit B to the Covered Software; or
30
+
31
+     b. that the Covered Software was made available under the terms of version
32
+        1.1 or earlier of the License, but not also under the terms of a
33
+        Secondary License.
34
+
35
+1.6. “Executable Form”
36
+
37
+     means any form of the work other than Source Code Form.
38
+
39
+1.7. “Larger Work”
40
+
41
+     means a work that combines Covered Software with other material, in a separate
42
+     file or files, that is not Covered Software.
43
+
44
+1.8. “License”
45
+
46
+     means this document.
47
+
48
+1.9. “Licensable”
49
+
50
+     means having the right to grant, to the maximum extent possible, whether at the
51
+     time of the initial grant or subsequently, any and all of the rights conveyed by
52
+     this License.
53
+
54
+1.10. “Modifications”
55
+
56
+     means any of the following:
57
+
58
+     a. any file in Source Code Form that results from an addition to, deletion
59
+        from, or modification of the contents of Covered Software; or
60
+
61
+     b. any new file in Source Code Form that contains any Covered Software.
62
+
63
+1.11. “Patent Claims” of a Contributor
64
+
65
+      means any patent claim(s), including without limitation, method, process,
66
+      and apparatus claims, in any patent Licensable by such Contributor that
67
+      would be infringed, but for the grant of the License, by the making,
68
+      using, selling, offering for sale, having made, import, or transfer of
69
+      either its Contributions or its Contributor Version.
70
+
71
+1.12. “Secondary License”
72
+
73
+      means either the GNU General Public License, Version 2.0, the GNU Lesser
74
+      General Public License, Version 2.1, the GNU Affero General Public
75
+      License, Version 3.0, or any later versions of those licenses.
76
+
77
+1.13. “Source Code Form”
78
+
79
+      means the form of the work preferred for making modifications.
80
+
81
+1.14. “You” (or “Your”)
82
+
83
+      means an individual or a legal entity exercising rights under this
84
+      License. For legal entities, “You” includes any entity that controls, is
85
+      controlled by, or is under common control with You. For purposes of this
86
+      definition, “control” means (a) the power, direct or indirect, to cause
87
+      the direction or management of such entity, whether by contract or
88
+      otherwise, or (b) ownership of more than fifty percent (50%) of the
89
+      outstanding shares or beneficial ownership of such entity.
90
+
91
+
92
+2. License Grants and Conditions
93
+
94
+2.1. Grants
95
+
96
+     Each Contributor hereby grants You a world-wide, royalty-free,
97
+     non-exclusive license:
98
+
99
+     a. under intellectual property rights (other than patent or trademark)
100
+        Licensable by such Contributor to use, reproduce, make available,
101
+        modify, display, perform, distribute, and otherwise exploit its
102
+        Contributions, either on an unmodified basis, with Modifications, or as
103
+        part of a Larger Work; and
104
+
105
+     b. under Patent Claims of such Contributor to make, use, sell, offer for
106
+        sale, have made, import, and otherwise transfer either its Contributions
107
+        or its Contributor Version.
108
+
109
+2.2. Effective Date
110
+
111
+     The licenses granted in Section 2.1 with respect to any Contribution become
112
+     effective for each Contribution on the date the Contributor first distributes
113
+     such Contribution.
114
+
115
+2.3. Limitations on Grant Scope
116
+
117
+     The licenses granted in this Section 2 are the only rights granted under this
118
+     License. No additional rights or licenses will be implied from the distribution
119
+     or licensing of Covered Software under this License. Notwithstanding Section
120
+     2.1(b) above, no patent license is granted by a Contributor:
121
+
122
+     a. for any code that a Contributor has removed from Covered Software; or
123
+
124
+     b. for infringements caused by: (i) Your and any other third party’s
125
+        modifications of Covered Software, or (ii) the combination of its
126
+        Contributions with other software (except as part of its Contributor
127
+        Version); or
128
+
129
+     c. under Patent Claims infringed by Covered Software in the absence of its
130
+        Contributions.
131
+
132
+     This License does not grant any rights in the trademarks, service marks, or
133
+     logos of any Contributor (except as may be necessary to comply with the
134
+     notice requirements in Section 3.4).
135
+
136
+2.4. Subsequent Licenses
137
+
138
+     No Contributor makes additional grants as a result of Your choice to
139
+     distribute the Covered Software under a subsequent version of this License
140
+     (see Section 10.2) or under the terms of a Secondary License (if permitted
141
+     under the terms of Section 3.3).
142
+
143
+2.5. Representation
144
+
145
+     Each Contributor represents that the Contributor believes its Contributions
146
+     are its original creation(s) or it has sufficient rights to grant the
147
+     rights to its Contributions conveyed by this License.
148
+
149
+2.6. Fair Use
150
+
151
+     This License is not intended to limit any rights You have under applicable
152
+     copyright doctrines of fair use, fair dealing, or other equivalents.
153
+
154
+2.7. Conditions
155
+
156
+     Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
157
+     Section 2.1.
158
+
159
+
160
+3. Responsibilities
161
+
162
+3.1. Distribution of Source Form
163
+
164
+     All distribution of Covered Software in Source Code Form, including any
165
+     Modifications that You create or to which You contribute, must be under the
166
+     terms of this License. You must inform recipients that the Source Code Form
167
+     of the Covered Software is governed by the terms of this License, and how
168
+     they can obtain a copy of this License. You may not attempt to alter or
169
+     restrict the recipients’ rights in the Source Code Form.
170
+
171
+3.2. Distribution of Executable Form
172
+
173
+     If You distribute Covered Software in Executable Form then:
174
+
175
+     a. such Covered Software must also be made available in Source Code Form,
176
+        as described in Section 3.1, and You must inform recipients of the
177
+        Executable Form how they can obtain a copy of such Source Code Form by
178
+        reasonable means in a timely manner, at a charge no more than the cost
179
+        of distribution to the recipient; and
180
+
181
+     b. You may distribute such Executable Form under the terms of this License,
182
+        or sublicense it under different terms, provided that the license for
183
+        the Executable Form does not attempt to limit or alter the recipients’
184
+        rights in the Source Code Form under this License.
185
+
186
+3.3. Distribution of a Larger Work
187
+
188
+     You may create and distribute a Larger Work under terms of Your choice,
189
+     provided that You also comply with the requirements of this License for the
190
+     Covered Software. If the Larger Work is a combination of Covered Software
191
+     with a work governed by one or more Secondary Licenses, and the Covered
192
+     Software is not Incompatible With Secondary Licenses, this License permits
193
+     You to additionally distribute such Covered Software under the terms of
194
+     such Secondary License(s), so that the recipient of the Larger Work may, at
195
+     their option, further distribute the Covered Software under the terms of
196
+     either this License or such Secondary License(s).
197
+
198
+3.4. Notices
199
+
200
+     You may not remove or alter the substance of any license notices (including
201
+     copyright notices, patent notices, disclaimers of warranty, or limitations
202
+     of liability) contained within the Source Code Form of the Covered
203
+     Software, except that You may alter any license notices to the extent
204
+     required to remedy known factual inaccuracies.
205
+
206
+3.5. Application of Additional Terms
207
+
208
+     You may choose to offer, and to charge a fee for, warranty, support,
209
+     indemnity or liability obligations to one or more recipients of Covered
210
+     Software. However, You may do so only on Your own behalf, and not on behalf
211
+     of any Contributor. You must make it absolutely clear that any such
212
+     warranty, support, indemnity, or liability obligation is offered by You
213
+     alone, and You hereby agree to indemnify every Contributor for any
214
+     liability incurred by such Contributor as a result of warranty, support,
215
+     indemnity or liability terms You offer. You may include additional
216
+     disclaimers of warranty and limitations of liability specific to any
217
+     jurisdiction.
218
+
219
+4. Inability to Comply Due to Statute or Regulation
220
+
221
+   If it is impossible for You to comply with any of the terms of this License
222
+   with respect to some or all of the Covered Software due to statute, judicial
223
+   order, or regulation then You must: (a) comply with the terms of this License
224
+   to the maximum extent possible; and (b) describe the limitations and the code
225
+   they affect. Such description must be placed in a text file included with all
226
+   distributions of the Covered Software under this License. Except to the
227
+   extent prohibited by statute or regulation, such description must be
228
+   sufficiently detailed for a recipient of ordinary skill to be able to
229
+   understand it.
230
+
231
+5. Termination
232
+
233
+5.1. The rights granted under this License will terminate automatically if You
234
+     fail to comply with any of its terms. However, if You become compliant,
235
+     then the rights granted under this License from a particular Contributor
236
+     are reinstated (a) provisionally, unless and until such Contributor
237
+     explicitly and finally terminates Your grants, and (b) on an ongoing basis,
238
+     if such Contributor fails to notify You of the non-compliance by some
239
+     reasonable means prior to 60 days after You have come back into compliance.
240
+     Moreover, Your grants from a particular Contributor are reinstated on an
241
+     ongoing basis if such Contributor notifies You of the non-compliance by
242
+     some reasonable means, this is the first time You have received notice of
243
+     non-compliance with this License from such Contributor, and You become
244
+     compliant prior to 30 days after Your receipt of the notice.
245
+
246
+5.2. If You initiate litigation against any entity by asserting a patent
247
+     infringement claim (excluding declaratory judgment actions, counter-claims,
248
+     and cross-claims) alleging that a Contributor Version directly or
249
+     indirectly infringes any patent, then the rights granted to You by any and
250
+     all Contributors for the Covered Software under Section 2.1 of this License
251
+     shall terminate.
252
+
253
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
254
+     license agreements (excluding distributors and resellers) which have been
255
+     validly granted by You or Your distributors under this License prior to
256
+     termination shall survive termination.
257
+
258
+6. Disclaimer of Warranty
259
+
260
+   Covered Software is provided under this License on an “as is” basis, without
261
+   warranty of any kind, either expressed, implied, or statutory, including,
262
+   without limitation, warranties that the Covered Software is free of defects,
263
+   merchantable, fit for a particular purpose or non-infringing. The entire
264
+   risk as to the quality and performance of the Covered Software is with You.
265
+   Should any Covered Software prove defective in any respect, You (not any
266
+   Contributor) assume the cost of any necessary servicing, repair, or
267
+   correction. This disclaimer of warranty constitutes an essential part of this
268
+   License. No use of  any Covered Software is authorized under this License
269
+   except under this disclaimer.
270
+
271
+7. Limitation of Liability
272
+
273
+   Under no circumstances and under no legal theory, whether tort (including
274
+   negligence), contract, or otherwise, shall any Contributor, or anyone who
275
+   distributes Covered Software as permitted above, be liable to You for any
276
+   direct, indirect, special, incidental, or consequential damages of any
277
+   character including, without limitation, damages for lost profits, loss of
278
+   goodwill, work stoppage, computer failure or malfunction, or any and all
279
+   other commercial damages or losses, even if such party shall have been
280
+   informed of the possibility of such damages. This limitation of liability
281
+   shall not apply to liability for death or personal injury resulting from such
282
+   party’s negligence to the extent applicable law prohibits such limitation.
283
+   Some jurisdictions do not allow the exclusion or limitation of incidental or
284
+   consequential damages, so this exclusion and limitation may not apply to You.
285
+
286
+8. Litigation
287
+
288
+   Any litigation relating to this License may be brought only in the courts of
289
+   a jurisdiction where the defendant maintains its principal place of business
290
+   and such litigation shall be governed by laws of that jurisdiction, without
291
+   reference to its conflict-of-law provisions. Nothing in this Section shall
292
+   prevent a party’s ability to bring cross-claims or counter-claims.
293
+
294
+9. Miscellaneous
295
+
296
+   This License represents the complete agreement concerning the subject matter
297
+   hereof. If any provision of this License is held to be unenforceable, such
298
+   provision shall be reformed only to the extent necessary to make it
299
+   enforceable. Any law or regulation which provides that the language of a
300
+   contract shall be construed against the drafter shall not be used to construe
301
+   this License against a Contributor.
302
+
303
+
304
+10. Versions of the License
305
+
306
+10.1. New Versions
307
+
308
+      Mozilla Foundation is the license steward. Except as provided in Section
309
+      10.3, no one other than the license steward has the right to modify or
310
+      publish new versions of this License. Each version will be given a
311
+      distinguishing version number.
312
+
313
+10.2. Effect of New Versions
314
+
315
+      You may distribute the Covered Software under the terms of the version of
316
+      the License under which You originally received the Covered Software, or
317
+      under the terms of any subsequent version published by the license
318
+      steward.
319
+
320
+10.3. Modified Versions
321
+
322
+      If you create software not governed by this License, and you want to
323
+      create a new license for such software, you may create and use a modified
324
+      version of this License if you rename the license and remove any
325
+      references to the name of the license steward (except to note that such
326
+      modified license differs from this License).
327
+
328
+10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
329
+      If You choose to distribute Source Code Form that is Incompatible With
330
+      Secondary Licenses under the terms of this version of the License, the
331
+      notice described in Exhibit B of this License must be attached.
332
+
333
+Exhibit A - Source Code Form License Notice
334
+
335
+      This Source Code Form is subject to the
336
+      terms of the Mozilla Public License, v.
337
+      2.0. If a copy of the MPL was not
338
+      distributed with this file, You can
339
+      obtain one at
340
+      http://mozilla.org/MPL/2.0/.
341
+
342
+If it is not possible or desirable to put the notice in a particular file, then
343
+You may include the notice in a location (such as a LICENSE file in a relevant
344
+directory) where a recipient would be likely to look for such a notice.
345
+
346
+You may add additional accurate notices of copyright ownership.
347
+
348
+Exhibit B - “Incompatible With Secondary Licenses” Notice
349
+
350
+      This Source Code Form is “Incompatible
351
+      With Secondary Licenses”, as defined by
352
+      the Mozilla Public License, v. 2.0.
353
+
0 354
new file mode 100644
... ...
@@ -0,0 +1,89 @@
0
+# errwrap
1
+
2
+`errwrap` is a package for Go that formalizes the pattern of wrapping errors
3
+and checking if an error contains another error.
4
+
5
+There is a common pattern in Go of taking a returned `error` value and
6
+then wrapping it (such as with `fmt.Errorf`) before returning it. The problem
7
+with this pattern is that you completely lose the original `error` structure.
8
+
9
+Arguably the _correct_ approach is that you should make a custom structure
10
+implementing the `error` interface, and have the original error as a field
11
+on that structure, such [as this example](http://golang.org/pkg/os/#PathError).
12
+This is a good approach, but you have to know the entire chain of possible
13
+rewrapping that happens, when you might just care about one.
14
+
15
+`errwrap` formalizes this pattern (it doesn't matter what approach you use
16
+above) by giving a single interface for wrapping errors, checking if a specific
17
+error is wrapped, and extracting that error.
18
+
19
+## Installation and Docs
20
+
21
+Install using `go get github.com/hashicorp/errwrap`.
22
+
23
+Full documentation is available at
24
+http://godoc.org/github.com/hashicorp/errwrap
25
+
26
+## Usage
27
+
28
+#### Basic Usage
29
+
30
+Below is a very basic example of its usage:
31
+
32
+```go
33
+// A function that always returns an error, but wraps it, like a real
34
+// function might.
35
+func tryOpen() error {
36
+	_, err := os.Open("/i/dont/exist")
37
+	if err != nil {
38
+		return errwrap.Wrapf("Doesn't exist: {{err}}", err)
39
+	}
40
+
41
+	return nil
42
+}
43
+
44
+func main() {
45
+	err := tryOpen()
46
+
47
+	// We can use the Contains helpers to check if an error contains
48
+	// another error. It is safe to do this with a nil error, or with
49
+	// an error that doesn't even use the errwrap package.
50
+	if errwrap.Contains(err, "does not exist") {
51
+		// Do something
52
+	}
53
+	if errwrap.ContainsType(err, new(os.PathError)) {
54
+		// Do something
55
+	}
56
+
57
+	// Or we can use the associated `Get` functions to just extract
58
+	// a specific error. This would return nil if that specific error doesn't
59
+	// exist.
60
+	perr := errwrap.GetType(err, new(os.PathError))
61
+}
62
+```
63
+
64
+#### Custom Types
65
+
66
+If you're already making custom types that properly wrap errors, then
67
+you can get all the functionality of `errwraps.Contains` and such by
68
+implementing the `Wrapper` interface with just one function. Example:
69
+
70
+```go
71
+type AppError {
72
+  Code ErrorCode
73
+  Err  error
74
+}
75
+
76
+func (e *AppError) WrappedErrors() []error {
77
+  return []error{e.Err}
78
+}
79
+```
80
+
81
+Now this works:
82
+
83
+```go
84
+err := &AppError{Err: fmt.Errorf("an error")}
85
+if errwrap.ContainsType(err, fmt.Errorf("")) {
86
+	// This will work!
87
+}
88
+```
0 89
new file mode 100644
... ...
@@ -0,0 +1,169 @@
0
+// Package errwrap implements methods to formalize error wrapping in Go.
1
+//
2
+// All of the top-level functions that take an `error` are built to be able
3
+// to take any error, not just wrapped errors. This allows you to use errwrap
4
+// without having to type-check and type-cast everywhere.
5
+package errwrap
6
+
7
+import (
8
+	"errors"
9
+	"reflect"
10
+	"strings"
11
+)
12
+
13
+// WalkFunc is the callback called for Walk.
14
+type WalkFunc func(error)
15
+
16
+// Wrapper is an interface that can be implemented by custom types to
17
+// have all the Contains, Get, etc. functions in errwrap work.
18
+//
19
+// When Walk reaches a Wrapper, it will call the callback for every
20
+// wrapped error in addition to the wrapper itself. Since all the top-level
21
+// functions in errwrap use Walk, this means that all those functions work
22
+// with your custom type.
23
+type Wrapper interface {
24
+	WrappedErrors() []error
25
+}
26
+
27
+// Wrap defines that outer wraps inner, returning an error type that
28
+// can be cleanly used with the other methods in this package, such as
29
+// Contains, GetAll, etc.
30
+//
31
+// This function won't modify the error message at all (the outer message
32
+// will be used).
33
+func Wrap(outer, inner error) error {
34
+	return &wrappedError{
35
+		Outer: outer,
36
+		Inner: inner,
37
+	}
38
+}
39
+
40
+// Wrapf wraps an error with a formatting message. This is similar to using
41
+// `fmt.Errorf` to wrap an error. If you're using `fmt.Errorf` to wrap
42
+// errors, you should replace it with this.
43
+//
44
+// format is the format of the error message. The string '{{err}}' will
45
+// be replaced with the original error message.
46
+func Wrapf(format string, err error) error {
47
+	outerMsg := "<nil>"
48
+	if err != nil {
49
+		outerMsg = err.Error()
50
+	}
51
+
52
+	outer := errors.New(strings.Replace(
53
+		format, "{{err}}", outerMsg, -1))
54
+
55
+	return Wrap(outer, err)
56
+}
57
+
58
+// Contains checks if the given error contains an error with the
59
+// message msg. If err is not a wrapped error, this will always return
60
+// false unless the error itself happens to match this msg.
61
+func Contains(err error, msg string) bool {
62
+	return len(GetAll(err, msg)) > 0
63
+}
64
+
65
+// ContainsType checks if the given error contains an error with
66
+// the same concrete type as v. If err is not a wrapped error, this will
67
+// check the err itself.
68
+func ContainsType(err error, v interface{}) bool {
69
+	return len(GetAllType(err, v)) > 0
70
+}
71
+
72
+// Get is the same as GetAll but returns the deepest matching error.
73
+func Get(err error, msg string) error {
74
+	es := GetAll(err, msg)
75
+	if len(es) > 0 {
76
+		return es[len(es)-1]
77
+	}
78
+
79
+	return nil
80
+}
81
+
82
+// GetType is the same as GetAllType but returns the deepest matching error.
83
+func GetType(err error, v interface{}) error {
84
+	es := GetAllType(err, v)
85
+	if len(es) > 0 {
86
+		return es[len(es)-1]
87
+	}
88
+
89
+	return nil
90
+}
91
+
92
+// GetAll gets all the errors that might be wrapped in err with the
93
+// given message. The order of the errors is such that the outermost
94
+// matching error (the most recent wrap) is index zero, and so on.
95
+func GetAll(err error, msg string) []error {
96
+	var result []error
97
+
98
+	Walk(err, func(err error) {
99
+		if err.Error() == msg {
100
+			result = append(result, err)
101
+		}
102
+	})
103
+
104
+	return result
105
+}
106
+
107
+// GetAllType gets all the errors that are the same type as v.
108
+//
109
+// The order of the return value is the same as described in GetAll.
110
+func GetAllType(err error, v interface{}) []error {
111
+	var result []error
112
+
113
+	var search string
114
+	if v != nil {
115
+		search = reflect.TypeOf(v).String()
116
+	}
117
+	Walk(err, func(err error) {
118
+		var needle string
119
+		if err != nil {
120
+			needle = reflect.TypeOf(err).String()
121
+		}
122
+
123
+		if needle == search {
124
+			result = append(result, err)
125
+		}
126
+	})
127
+
128
+	return result
129
+}
130
+
131
+// Walk walks all the wrapped errors in err and calls the callback. If
132
+// err isn't a wrapped error, this will be called once for err. If err
133
+// is a wrapped error, the callback will be called for both the wrapper
134
+// that implements error as well as the wrapped error itself.
135
+func Walk(err error, cb WalkFunc) {
136
+	if err == nil {
137
+		return
138
+	}
139
+
140
+	switch e := err.(type) {
141
+	case *wrappedError:
142
+		cb(e.Outer)
143
+		Walk(e.Inner, cb)
144
+	case Wrapper:
145
+		cb(err)
146
+
147
+		for _, err := range e.WrappedErrors() {
148
+			Walk(err, cb)
149
+		}
150
+	default:
151
+		cb(err)
152
+	}
153
+}
154
+
155
+// wrappedError is an implementation of error that has both the
156
+// outer and inner errors.
157
+type wrappedError struct {
158
+	Outer error
159
+	Inner error
160
+}
161
+
162
+func (w *wrappedError) Error() string {
163
+	return w.Outer.Error()
164
+}
165
+
166
+func (w *wrappedError) WrappedErrors() []error {
167
+	return []error{w.Outer, w.Inner}
168
+}
0 169
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+module github.com/hashicorp/errwrap
... ...
@@ -1,5 +1,11 @@
1 1
 # go-multierror
2 2
 
3
+[![Build Status](http://img.shields.io/travis/hashicorp/go-multierror.svg?style=flat-square)][travis]
4
+[![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs]
5
+
6
+[travis]: https://travis-ci.org/hashicorp/go-multierror
7
+[godocs]: https://godoc.org/github.com/hashicorp/go-multierror
8
+
3 9
 `go-multierror` is a package for Go that provides a mechanism for
4 10
 representing a list of `error` values as a single `error`.
5 11
 
... ...
@@ -14,7 +14,20 @@ func Append(err error, errs ...error) *Error {
14 14
 			err = new(Error)
15 15
 		}
16 16
 
17
-		err.Errors = append(err.Errors, errs...)
17
+		// Go through each error and flatten
18
+		for _, e := range errs {
19
+			switch e := e.(type) {
20
+			case *Error:
21
+				if e != nil {
22
+					err.Errors = append(err.Errors, e.Errors...)
23
+				}
24
+			default:
25
+				if e != nil {
26
+					err.Errors = append(err.Errors, e)
27
+				}
28
+			}
29
+		}
30
+
18 31
 		return err
19 32
 	default:
20 33
 		newErrs := make([]error, 0, len(errs)+1)
... ...
@@ -23,8 +36,6 @@ func Append(err error, errs ...error) *Error {
23 23
 		}
24 24
 		newErrs = append(newErrs, errs...)
25 25
 
26
-		return &Error{
27
-			Errors: newErrs,
28
-		}
26
+		return Append(&Error{}, newErrs...)
29 27
 	}
30 28
 }
31 29
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+package multierror
1
+
2
+// Flatten flattens the given error, merging any *Errors together into
3
+// a single *Error.
4
+func Flatten(err error) error {
5
+	// If it isn't an *Error, just return the error as-is
6
+	if _, ok := err.(*Error); !ok {
7
+		return err
8
+	}
9
+
10
+	// Otherwise, make the result and flatten away!
11
+	flatErr := new(Error)
12
+	flatten(err, flatErr)
13
+	return flatErr
14
+}
15
+
16
+func flatten(err error, flatErr *Error) {
17
+	switch err := err.(type) {
18
+	case *Error:
19
+		for _, e := range err.Errors {
20
+			flatten(e, flatErr)
21
+		}
22
+	default:
23
+		flatErr.Errors = append(flatErr.Errors, err)
24
+	}
25
+}
... ...
@@ -12,12 +12,16 @@ type ErrorFormatFunc func([]error) string
12 12
 // ListFormatFunc is a basic formatter that outputs the number of errors
13 13
 // that occurred along with a bullet point list of the errors.
14 14
 func ListFormatFunc(es []error) string {
15
+	if len(es) == 1 {
16
+		return fmt.Sprintf("1 error occurred:\n\t* %s\n\n", es[0])
17
+	}
18
+
15 19
 	points := make([]string, len(es))
16 20
 	for i, err := range es {
17 21
 		points[i] = fmt.Sprintf("* %s", err)
18 22
 	}
19 23
 
20 24
 	return fmt.Sprintf(
21
-		"%d error(s) occurred:\n\n%s",
22
-		len(es), strings.Join(points, "\n"))
25
+		"%d errors occurred:\n\t%s\n\n",
26
+		len(es), strings.Join(points, "\n\t"))
23 27
 }
24 28
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+module github.com/hashicorp/go-multierror
1
+
2
+require github.com/hashicorp/errwrap v1.0.0
... ...
@@ -40,11 +40,11 @@ func (e *Error) GoString() string {
40 40
 }
41 41
 
42 42
 // WrappedErrors returns the list of errors that this Error is wrapping.
43
-// It is an implementatin of the errwrap.Wrapper interface so that
43
+// It is an implementation of the errwrap.Wrapper interface so that
44 44
 // multierror.Error can be used with that library.
45 45
 //
46 46
 // This method is not safe to be called concurrently and is no different
47
-// than accessing the Errors field directly. It is implementd only to
47
+// than accessing the Errors field directly. It is implemented only to
48 48
 // satisfy the errwrap.Wrapper interface.
49 49
 func (e *Error) WrappedErrors() []error {
50 50
 	return e.Errors
51 51
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+package multierror
1
+
2
+import (
3
+	"fmt"
4
+
5
+	"github.com/hashicorp/errwrap"
6
+)
7
+
8
+// Prefix is a helper function that will prefix some text
9
+// to the given error. If the error is a multierror.Error, then
10
+// it will be prefixed to each wrapped error.
11
+//
12
+// This is useful to use when appending multiple multierrors
13
+// together in order to give better scoping.
14
+func Prefix(err error, prefix string) error {
15
+	if err == nil {
16
+		return nil
17
+	}
18
+
19
+	format := fmt.Sprintf("%s {{err}}", prefix)
20
+	switch err := err.(type) {
21
+	case *Error:
22
+		// Typed nils can reach here, so initialize if we are nil
23
+		if err == nil {
24
+			err = new(Error)
25
+		}
26
+
27
+		// Wrap each of the errors
28
+		for i, e := range err.Errors {
29
+			err.Errors[i] = errwrap.Wrapf(format, e)
30
+		}
31
+
32
+		return err
33
+	default:
34
+		return errwrap.Wrapf(format, err)
35
+	}
36
+}
0 37
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+package multierror
1
+
2
+// Len implements sort.Interface function for length
3
+func (err Error) Len() int {
4
+	return len(err.Errors)
5
+}
6
+
7
+// Swap implements sort.Interface function for swapping elements
8
+func (err Error) Swap(i, j int) {
9
+	err.Errors[i], err.Errors[j] = err.Errors[j], err.Errors[i]
10
+}
11
+
12
+// Less implements sort.Interface function for determining order
13
+func (err Error) Less(i, j int) bool {
14
+	return err.Errors[i].Error() < err.Errors[j].Error()
15
+}