-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
96 lines (87 loc) · 1.62 KB
/
error.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
package e5
import (
"errors"
"strings"
)
// Error represents multiple errors
type Error []error
func (c Error) copy() Error {
ret := make(Error, len(c))
copy(ret, c)
return ret
}
// Is reports whether any error in the slice matches target
func (c Error) Is(target error) bool {
for _, err := range c {
if errors.Is(err, target) {
return true
}
}
return false
}
// As reports whether any error in the slice matches target.
// And if so, assign the first matching error to target
func (c Error) As(target interface{}) bool {
for _, err := range c {
if errors.As(err, target) {
return true
}
}
return false
}
// Unwrap returns all errors
func (c Error) Unwrap() []error {
return c
}
// Error implements error interface
func (c Error) Error() string {
var b strings.Builder
for i, err := range c {
str := err.Error()
if i > 0 && len(str) > 0 && b.Len() > 0 {
b.WriteString("\n")
}
b.WriteString(str)
}
return b.String()
}
// Join joins two errors
func Join(err error, prev error) Error {
e1, ok1 := err.(Error)
e2, ok2 := prev.(Error)
if ok1 && !ok2 {
if e1.Is(prev) {
return e1
}
return append(e1.copy(), prev)
}
if !ok1 && ok2 {
if e2.Is(err) {
return e2
}
return append(e2.copy(), err)
}
if !ok1 && !ok2 {
if errors.Is(err, prev) {
return Error{err}
}
return Error{err, prev}
}
e1 = e1.copy()
for _, e := range e2 {
if e1.Is(e) {
continue
}
e1 = append(e1, e)
}
return e1
}
// With returns a WrapFunc that wraps an error value
func With(err error) WrapFunc {
return func(prev error) error {
if prev == nil {
return nil
}
return Join(err, prev)
}
}