-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
75 lines (67 loc) · 1.3 KB
/
errors.go
File metadata and controls
75 lines (67 loc) · 1.3 KB
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
package flago
import (
"errors"
"strings"
)
type joinedErrors []error
func joinErr(errs ...error) error {
n := 0
for _, err := range errs {
if err != nil {
n++
}
}
if n == 0 {
return nil
}
if n == 1 {
for _, err := range errs {
if err != nil {
return err
}
}
}
joinedErrors := make(joinedErrors, 0, n)
for _, err := range errs {
if err != nil {
joinedErrors = append(joinedErrors, err)
}
}
return joinedErrors
}
func (e joinedErrors) Error() string {
if len(e) == 1 {
return e[0].Error()
}
sb := strings.Builder{}
sb.WriteString(e[0].Error())
for _, err := range e[1:] {
sb.WriteString("\n")
sb.WriteString(err.Error())
}
return sb.String()
}
//goland:noinspection GoStandardMethods
func (e joinedErrors) Unwrap() []error {
return e
}
// Is returns true if any of the errors in the joinedErrors is target (according to errors.Is() logic).
// It's needed make joinedErrors compatible with errors.Is()
func (e joinedErrors) Is(target error) bool {
for _, err := range e {
if errors.Is(err, target) {
return true
}
}
return false
}
// As makes joinedErrors compatible with errors.As()
func (e joinedErrors) As(target any) bool {
for _, err := range e {
if //goland:noinspection GoErrorsAs
errors.As(err, target) {
return true
}
}
return false
}