-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.go
55 lines (51 loc) · 1.52 KB
/
combine.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
package errs
// import "errors"
// // Combine returns a MultiError error for 2 or more errors that are not nil,
// // or the same error if only one error was passed,
// // or nil if zero arguments are passed or all passed errors are nil.
// //
// // The MultiError Error method returns the strings from the
// // individual Error methods joined by the new line character '\n'.
// //
// // In case of a MultiError, errors.Is and errors.As will return true
// // for the first matched error.
// //
// // Combine does not wrap the passed errors with a text or call stack.
// //
// // The motivation behind Combine and MultiError is to combine different
// // logical errors into one, as compared to error wrapping
// // which adds more information to one logical error.
// func Combine(errs ...error) error {
// var combined multiError
// for _, err := range errs {
// if err == nil {
// continue
// }
// var m multiError
// if errors.As(err, &m) {
// combined = append(combined, m.Errors()...)
// } else {
// combined = append(combined, err)
// }
// }
// switch len(combined) {
// case 0:
// return nil
// case 1:
// return combined[0]
// }
// return combined
// }
// // Uncombine returns multiple errors if err is a MultiError,
// // else it will return a single element slice containing err
// // or nil if err is nil.
// func Uncombine(err error) []error {
// if err == nil {
// return nil
// }
// var multi MultiError
// if errors.As(err, &multi) {
// return multi.Errors()
// }
// return []error{err}
// }