-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
104 lines (84 loc) · 1.71 KB
/
Copy patherror.go
File metadata and controls
104 lines (84 loc) · 1.71 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
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
package vaddie
import (
"errors"
"strconv"
"strings"
)
// ValidationError is what we return on invalid validations.
// Output of the error is a single string of:
// `Key [Index] Message [(Help)]`
type ValidationError struct {
Key string
Message string
Help string
Index *int
}
func (v *ValidationError) Error() string {
if v == nil {
return ""
}
sb := &strings.Builder{}
sb.WriteString(v.Key)
if v.Index != nil {
sb.WriteString("[")
sb.WriteString(strconv.Itoa(*v.Index))
sb.WriteString("]")
}
sb.WriteString(" ")
sb.WriteString(v.Message)
if v.Help != "" {
sb.WriteString(" ( ")
sb.WriteString(v.Help)
sb.WriteString(" )")
}
return sb.String()
}
func expandErrorKey(err error, key string) error {
ve, isValidationError := err.(*ValidationError)
if !isValidationError {
return &ValidationError{
Message: err.Error(),
Key: key,
}
}
ve.Key = key
return ve
}
func expandErrorIndex(err error, index int) error {
ve, isValidationError := err.(*ValidationError)
if !isValidationError {
return &ValidationError{
Message: err.Error(),
Index: &index,
}
}
ve.Index = &index
return ve
}
func expandErrorKeyIndex(err error, key string, index int) error {
ve, isValidationError := err.(*ValidationError)
if !isValidationError {
return &ValidationError{
Message: err.Error(),
Index: &index,
Key: key,
}
}
ve.Index = &index
ve.Key = key
return ve
}
// Join combines all errors into one.
func Join(errs ...error) error {
return errors.Join(errs...)
}
// JoinAnd will return the first non-nil error,
// or nil if all errors are nil.
func JoinAnd(errs ...error) error {
for _, e := range errs {
if e != nil {
return e
}
}
return nil
}