-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
63 lines (53 loc) · 1.31 KB
/
errors.go
File metadata and controls
63 lines (53 loc) · 1.31 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
package structwalker
import (
"errors"
"fmt"
"strings"
)
var (
// ErrSkipField causes the walker to skip remaining handlers on the current field.
ErrSkipField = errors.New("skip field")
// ErrSkipChildren prevents recursion into a nested struct field.
ErrSkipChildren = errors.New("skip children")
)
// WalkError wraps an error with field path context.
type WalkError struct {
Path string
TagKey string
Field string
Err error
}
func (e *WalkError) Error() string {
if e.TagKey != "" {
return fmt.Sprintf("structwalker: %s (tag %q): %v", e.Path, e.TagKey, e.Err)
}
return fmt.Sprintf("structwalker: %s: %v", e.Path, e.Err)
}
func (e *WalkError) Unwrap() error {
return e.Err
}
// MultiError collects multiple errors from a walk (used with CollectErrors mode).
type MultiError struct {
Errors []*WalkError
}
func (e *MultiError) Error() string {
if len(e.Errors) == 1 {
return e.Errors[0].Error()
}
var b strings.Builder
fmt.Fprintf(&b, "structwalker: %d errors:\n", len(e.Errors))
for _, err := range e.Errors {
fmt.Fprintf(&b, " - %s\n", err.Error())
}
return b.String()
}
func (e *MultiError) Unwrap() []error {
errs := make([]error, len(e.Errors))
for i, err := range e.Errors {
errs[i] = err
}
return errs
}
func (e *MultiError) add(err *WalkError) {
e.Errors = append(e.Errors, err)
}