-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
76 lines (60 loc) · 1.84 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
package gonfig
import (
"errors"
"reflect"
"strings"
)
var (
// Can not handle specified type
ErrUnsupportedType = errors.New("unsupported type")
// Only ".json", ".yml", ".yaml" and ".env" file types are supported
ErrUnsupportedFileExt = errors.New("unsupported file extension")
// Provider could not find value with specified key
ErrKeyNotFound = errors.New("key not found")
// Field is required but no value provided
ErrRequiredField = errors.New("field is required")
// Could not parse the string value
ErrParsing = errors.New("failed parsing")
// Value overflows type
ErrValueOverflow = errors.New("value overflow")
)
const (
unsupportedTypeErrFormat = `%w: cannot handle type "%v" at "%v"`
unsupportedFileExtErrFormat = `%w: %v`
decodeFailedErrFormat = `failed to decode: %w`
requiredFieldErrFormat = `%w: no value found for "%v"`
unsupportedElementTypeErrFormat = `%w: cannot handle slice/array of "%v" at "%v"`
parseErrFormat = `%w at "%v": %v`
overflowErrFormat = `%w: "%v" overflows type "%v" at "%v"`
)
// An InvalidInputError describes an invalid argument passed to Into function
// The argument must be a non-nil struct pointer
type InvalidInputError struct {
Type reflect.Type
Value reflect.Value
}
func (e *InvalidInputError) Error() string {
msg := "gonfig: invalid input: "
if e.Type == nil {
msg += "nil"
} else if e.Type.Kind() != reflect.Ptr {
msg += "non-pointer type"
} else if e.Value.IsNil() {
msg += "nil pointer"
} else {
msg += "non-struct type"
}
return msg
}
// Collection of errors during populating the input struct
type ConfigErrors []error
func (ce ConfigErrors) Error() string {
if len(ce) == 0 {
return ""
}
msg := "gonfig:\n"
for i := range ce {
msg += " * " + ce[i].Error() + "\n"
}
return strings.TrimSpace(msg)
}