-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
78 lines (64 loc) · 1.69 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
package conf
import (
"errors"
"fmt"
"sort"
"strings"
)
// ErrFileNotFound is returned as a wrapped error by `Load` when the config file is
// not found in the given search dirs.
var ErrFileNotFound = fmt.Errorf("file not found")
// fieldErrors collects errors for fields of config struct.
type fieldErrors map[string]error
// Error formats all fields errors into a single string.
func (fe fieldErrors) Error() string {
keys := make([]string, 0, len(fe))
for key := range fe {
keys = append(keys, key)
}
sort.Strings(keys)
var sb strings.Builder
sb.Grow(len(keys) * 10)
for _, key := range keys {
sb.WriteString(key)
sb.WriteString(": ")
sb.WriteString(fe[key].Error())
sb.WriteString(", ")
}
return strings.TrimSuffix(sb.String(), ", ")
}
// Error implements the error interface and can represents multiple
// errors that occur in the course of a single decode.
type Error struct {
Errors []string
}
func (e *Error) Error() string {
points := make([]string, len(e.Errors))
for i, err := range e.Errors {
points[i] = fmt.Sprintf("* %s", err)
}
sort.Strings(points)
return fmt.Sprintf(
"%d error(s) decoding:\n\n%s",
len(e.Errors), strings.Join(points, "\n"))
}
// WrappedErrors implements the errwrap.Wrapper interface to make this
// return value more useful with the errwrap and go-multierror libraries.
func (e *Error) WrappedErrors() []error {
if e == nil {
return nil
}
result := make([]error, len(e.Errors))
for i, e := range e.Errors {
result[i] = errors.New(e)
}
return result
}
func appendErrors(errors []string, err error) []string {
switch e := err.(type) {
case *Error:
return append(errors, e.Errors...)
default:
return append(errors, e.Error())
}
}