-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
156 lines (127 loc) · 2.55 KB
/
errors.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
package goflow
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
type Error struct {
Filename string
Err error
}
func (e Error) Error() string {
return fmt.Sprintf("%s: %s", e.Filename, e.Err.Error())
}
func (e Error) Unwrap() error {
return e.Err
}
func (e Error) Format(s fmt.State, verb rune) {
if verb != 'v' || (!s.Flag('+')) {
fmt.Fprint(s, e.Error())
return
}
errMsg := fmt.Sprintf(" %v", e.Err)
var multiErr MultiError
if errors.As(e.Err, &multiErr) {
flat := multiErr.flatten()
strs := make([]string, len(flat.Errs))
for i, err := range flat.Errs {
strs[i] = fmt.Sprintf(" %v", err)
}
errMsg = strings.Join(strs, "\n")
}
fmt.Fprintf(s, `%s:
%v
`,
e.Filename,
errMsg,
)
}
type MultiError struct {
Errs []error
}
func (e MultiError) Error() string {
if len(e.Errs) == 1 {
return e.Errs[0].Error()
}
strs := make([]string, len(e.Errs))
for i, err := range e.Errs {
strs[i] = err.Error()
}
return fmt.Sprintf("[%s]", strings.Join(strs, ", "))
}
func (e MultiError) Is(err error) bool {
for _, sub := range e.Errs {
if errors.Is(sub, err) {
return true
}
}
return false
}
func (e MultiError) As(target interface{}) bool {
for _, sub := range e.Errs {
if errors.As(sub, target) {
return true
}
}
return false
}
func (e MultiError) flatten() MultiError {
errs := make([]error, 0, len(e.Errs))
for _, err := range e.Errs {
var me MultiError
if errors.As(err, &me) {
errs = append(errs, me.flatten().Errs...)
} else {
errs = append(errs, err)
}
}
return MultiError{Errs: errs}
}
type GraphError struct {
Wrapper string
Err error
}
func (e GraphError) Error() string {
return fmt.Sprintf("%v (%s)", e.Err, e.Wrapper)
}
func (e GraphError) Unwrap() error {
return e.Err
}
type NodeError struct {
ID string
Wrapper string
Err error
}
func (e NodeError) Error() string {
return fmt.Sprintf("%s: %v (%s)", e.ID, e.Err, e.Wrapper)
}
func (e NodeError) Unwrap() error {
return e.Err
}
var yamlErrorRegex = regexp.MustCompile(`(?s)(?:yaml: )?(?:unmarshal errors:.)?line (\d+): (.*)`)
type YAMLError struct {
Line int
Err error
}
func (e YAMLError) Error() string {
return fmt.Sprintf("line %d: %v (yaml)", e.Line, e.Err.Error())
}
func (e YAMLError) Unwrap() error {
return e.Err
}
func ParseYAMLError(err error) error {
if err == nil {
return nil
}
match := yamlErrorRegex.FindStringSubmatch(err.Error())
if len(match) == 0 {
return err
}
line, _ := strconv.Atoi(match[1])
return YAMLError{
Line: line,
Err: errors.New(strings.TrimSpace(match[2])),
}
}