-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
212 lines (178 loc) · 5.86 KB
/
Copy patherrors.go
File metadata and controls
212 lines (178 loc) · 5.86 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package libconfig
import (
"fmt"
"reflect"
)
// ErrCannotParseEnv is returned if the variable cannot be parsed into the type
// expected by the struct field, e.g. parsing "500" into int8 will return this.
// This indicates that either the struct field is the wrong type or that the
// data in the env var is simply incompatible with the type.
type ErrCannotParseEnv struct {
Because error
Kind reflect.Kind
Key string
Value string
}
// NewErrCannotParseEnv creates an ErrCannotParseEnv error
func NewErrCannotParseEnv(err error, k reflect.Kind, key, value string) *ErrCannotParseEnv {
return &ErrCannotParseEnv{
Because: err,
Kind: k,
Key: key,
Value: value,
}
}
// Error returns a human-readable description of the error
func (e *ErrCannotParseEnv) Error() string {
result := fmt.Sprintf("cannot parse env [%s] with value [%s] to kind [%s]", e.Key, e.Value, e.Kind)
if e.Because != nil {
result = fmt.Sprintf("%s: %s", result, e.Because.Error())
}
return result
}
// Cause returns the error that caused this ErrCannotParseEnv error
func (e *ErrCannotParseEnv) Cause() error {
return e.Because
}
// ErrCannotSetKind is returned if the kind of a field cannot be set from a string.
// This indicates that the logic is missing from `setValueFromString` to handle
// the given reflect.Kind.
type ErrCannotSetKind struct {
Kind reflect.Kind
}
// NewErrCannotSetKind creates a ErrCannotSetKind error
func NewErrCannotSetKind(k reflect.Kind) *ErrCannotSetKind {
return &ErrCannotSetKind{
Kind: k,
}
}
// Error returns a human-readable description of the error
func (e *ErrCannotSetKind) Error() string {
return fmt.Sprintf("cannot set kind [%s]", e.Kind.String())
}
// ErrDecodeFailure is returned by `Retrieve` if the value could not be decoded by the
// requested decoder
type ErrDecodeFailure struct {
Key string
Value string
Type string
Because error
}
// NewErrDecodeFailure creates a ErrDecodeFailure error which wraps the error describing
// the cause of the failure
func NewErrDecodeFailure(err error, key, value, typ string) *ErrDecodeFailure {
return &ErrDecodeFailure{
Key: key,
Value: value,
Type: typ,
Because: err,
}
}
// Error returns a human-readable description of the error
func (e *ErrDecodeFailure) Error() string {
result := fmt.Sprintf("failed to decode var [%s] with value [%s] as [%s]", e.Key, e.Value, e.Type)
if e.Because != nil {
result = fmt.Sprintf("%s: %s", result, e.Because.Error())
}
return result
}
// Cause returns the error that caused the ErrDecodeFailure
func (e *ErrDecodeFailure) Cause() error {
return e.Because
}
// ErrInvalidConfigType is returned if Get is called with a value that is not a pointer
// to a struct. It must be a pointer so that Get can modify the values. It must be a
// struct to have tagged fields.
type ErrInvalidConfigType struct {
Type reflect.Type
}
// NewErrInvalidConfigType creates an ErrInvalidConfigType
func NewErrInvalidConfigType(t reflect.Type) *ErrInvalidConfigType {
return &ErrInvalidConfigType{
Type: t,
}
}
// Error returns a human-readable description of the error
func (e *ErrInvalidConfigType) Error() string {
return fmt.Sprintf("config must be pointer to struct but got %s", e.Type.String())
}
// ErrInvalidTagOption is returned if the struct field tag has an unsupported option.
type ErrInvalidTagOption struct {
Tag string
BadOption string
}
// NewErrInvalidTagOption creates an ErrInvalidTagOption
func NewErrInvalidTagOption(tag, option string) *ErrInvalidTagOption {
return &ErrInvalidTagOption{
Tag: tag,
BadOption: option,
}
}
// Error returns a human-readable description of the error
func (e *ErrInvalidTagOption) Error() string {
return fmt.Sprintf("tag [%s] contains unsupported option [%s]", e.Tag, e.BadOption)
}
// ErrMissingNameTag is returned if the passed config struct field is tagged but no
// name is provided, e.g. `env:""`
type ErrMissingNameTag struct {
Tag string
}
// NewErrMissingNameTag create an ErrMissingNameTag
func NewErrMissingNameTag(t string) *ErrMissingNameTag {
return &ErrMissingNameTag{
Tag: t,
}
}
// Error ensures that ErrMissingNameTag conforms to the Error interface
func (e *ErrMissingNameTag) Error() string {
return fmt.Sprintf("tagged field must be named but got [%s]", e.Tag)
}
// ErrOverflow is returned if a numeric reflect.Value cannot be set because it would result in an overflow
type ErrOverflow struct {
Kind reflect.Kind
Key string
Value string
}
// NewErrOverflow creates an ErrOverflow
func NewErrOverflow(k reflect.Kind, key, value string) *ErrOverflow {
return &ErrOverflow{
Kind: k,
Key: key,
Value: value,
}
}
// Error returns a human-readable description of the error
func (e *ErrOverflow) Error() string {
return fmt.Sprintf("overflow detected trying to set field of kind [%s] to value [%s] for key [%s]", e.Kind.String(), e.Value, e.Key)
}
// ErrVarNotFound is returned if the given key is not found by the lookup function
type ErrVarNotFound struct {
Key string
}
// NewErrVarNotFound creates a ErrVarNotFound error
func NewErrVarNotFound(key string) *ErrVarNotFound {
return &ErrVarNotFound{
Key: key,
}
}
// Error returns a human-readable description of the error
func (e *ErrVarNotFound) Error() string {
return fmt.Sprintf("var not found for key [%s]", e.Key)
}
// ErrNestedTags is returned if a tagged struct contains a tagged field, which, if supported, could
// result in unexpected behavior due to the parsing order of structs and struct fields
type ErrNestedTags struct {
Field string
Key string
}
// NewErrNestedTags creates a ErrNestedTags error
func NewErrNestedTags(field, key string) *ErrNestedTags {
return &ErrNestedTags{
Field: field,
Key: key,
}
}
// Error returns a human-readable description of the error
func (e *ErrNestedTags) Error() string {
return fmt.Sprintf("field [%s] with key [%s] contains one or more nested subfields", e.Field, e.Key)
}