-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy patherrors.go
425 lines (370 loc) · 15.3 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Copyright 2020 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configreader
import (
"fmt"
"reflect"
"strings"
"github.com/cortexlabs/cortex/pkg/lib/errors"
s "github.com/cortexlabs/cortex/pkg/lib/strings"
)
const (
ErrParseConfig = "configreader.parse_config"
ErrUnsupportedFieldValidation = "configreader.unsupported_field_validation"
ErrUnsupportedKey = "configreader.unsupported_key"
ErrInvalidYAML = "configreader.invalid_yaml"
ErrTooLong = "configreader.too_long"
ErrTooShort = "configreader.too_short"
ErrLeadingWhitespace = "configreader.leading_whitespace"
ErrTrailingWhitespace = "configreader.trailing_whitespace"
ErrAlphaNumericDashUnderscore = "configreader.alpha_numeric_dash_underscore"
ErrAlphaNumericDashDotUnderscore = "configreader.alpha_numeric_dash_dot_underscore"
ErrInvalidAWSTag = "configreader.invalid_aws_tag"
ErrInvalidDockerImage = "configreader.invalid_docker_image"
ErrMustHavePrefix = "configreader.must_have_prefix"
ErrCantHavePrefix = "configreader.cant_have_prefix"
ErrInvalidInterface = "configreader.invalid_interface"
ErrInvalidFloat64 = "configreader.invalid_float64"
ErrInvalidFloat32 = "configreader.invalid_float32"
ErrInvalidInt64 = "configreader.invalid_int64"
ErrInvalidInt32 = "configreader.invalid_int32"
ErrInvalidInt = "configreader.invalid_int"
ErrInvalidStr = "configreader.invalid_str"
ErrDisallowedValue = "configreader.disallowed_value"
ErrMustBeLessThanOrEqualTo = "configreader.must_be_less_than_or_equal_to"
ErrMustBeLessThan = "configreader.must_be_less_than"
ErrMustBeGreaterThanOrEqualTo = "configreader.must_be_greater_than_or_equal_to"
ErrMustBeGreaterThan = "configreader.must_be_greater_than"
ErrIsNotMultiple = "configreader.is_not_multiple"
ErrNonStringKeyFound = "configreader.non_string_key_found"
ErrInvalidPrimitiveType = "configreader.invalid_primitive_type"
ErrDuplicatedValue = "configreader.duplicated_value"
ErrTooFewElements = "configreader.too_few_elements"
ErrTooManyElements = "configreader.too_many_elements"
ErrWrongNumberOfElements = "configreader.wrong_number_of_elements"
ErrCannotSetStructField = "configreader.cannot_set_struct_field"
ErrCannotBeNull = "configreader.cannot_be_null"
ErrCannotBeEmptyOrNull = "configreader.cannot_be_empty_or_null"
ErrCannotBeEmpty = "configreader.cannot_be_empty"
ErrMustBeDefined = "configreader.must_be_defined"
ErrMapMustBeDefined = "configreader.map_must_be_defined"
ErrMustBeEmpty = "configreader.must_be_empty"
ErrEmailTooLong = "configreader.email_too_long"
ErrEmailInvalid = "configreader.email_invalid"
ErrCortexResourceOnlyAllowed = "configreader.cortex_resource_only_allowed"
ErrCortexResourceNotAllowed = "configreader.cortex_resource_not_allowed"
ErrImageVersionMismatch = "configreader.image_version_mismatch"
)
func ErrorParseConfig() error {
return errors.WithStack(&errors.Error{
Kind: ErrParseConfig,
Message: fmt.Sprintf("failed to parse config file"),
})
}
func ErrorUnsupportedFieldValidation() error {
return errors.WithStack(&errors.Error{
Kind: ErrUnsupportedFieldValidation,
Message: fmt.Sprintf("undefined or unsupported field validation"),
})
}
func ErrorUnsupportedKey(key interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrUnsupportedKey,
Message: fmt.Sprintf("key %s is not supported", s.UserStr(key)),
})
}
func ErrorInvalidYAML(err error) error {
str := strings.TrimPrefix(errors.Message(err), "yaml: ")
return errors.WithStack(&errors.Error{
Kind: ErrInvalidYAML,
Message: fmt.Sprintf("invalid yaml: %s", str),
})
}
func ErrorTooLong(provided string, maxLen int) error {
return errors.WithStack(&errors.Error{
Kind: ErrTooLong,
Message: fmt.Sprintf("%s must be no more than %d characters", s.UserStr(provided), maxLen),
})
}
func ErrorTooShort(provided string, minLen int) error {
return errors.WithStack(&errors.Error{
Kind: ErrTooShort,
Message: fmt.Sprintf("%s must be at least %d characters", s.UserStr(provided), minLen),
})
}
func ErrorLeadingWhitespace(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrLeadingWhitespace,
Message: fmt.Sprintf("%s cannot start with whitespace", s.UserStr(provided)),
})
}
func ErrorTrailingWhitespace(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrTrailingWhitespace,
Message: fmt.Sprintf("%s cannot end with whitespace", s.UserStr(provided)),
})
}
func ErrorAlphaNumericDashUnderscore(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrAlphaNumericDashUnderscore,
Message: fmt.Sprintf("%s must contain only letters, numbers, underscores, and dashes", s.UserStr(provided)),
})
}
func ErrorAlphaNumericDashDotUnderscore(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrAlphaNumericDashDotUnderscore,
Message: fmt.Sprintf("%s must contain only letters, numbers, underscores, dashes, and periods", s.UserStr(provided)),
})
}
func ErrorInvalidAWSTag(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInvalidAWSTag,
Message: fmt.Sprintf("%s must contain only letters, numbers, spaces, and the following characters: _ . : / + - @", s.UserStr(provided)),
})
}
func ErrorInvalidDockerImage(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInvalidDockerImage,
Message: fmt.Sprintf("%s is not a valid docker image path", s.UserStr(provided)),
})
}
func ErrorMustHavePrefix(provided string, prefix string) error {
return errors.WithStack(&errors.Error{
Kind: ErrMustHavePrefix,
Message: fmt.Sprintf("%s must start with %s", s.UserStr(provided), s.UserStr(prefix)),
})
}
func ErrorCantHavePrefix(provided string, prefix string) error {
return errors.WithStack(&errors.Error{
Kind: ErrCantHavePrefix,
Message: fmt.Sprintf("%s cannot start with %s", s.UserStr(provided), s.UserStr(prefix)),
})
}
func ErrorInvalidInterface(provided interface{}, allowed interface{}, allowedVals ...interface{}) error {
allAllowedVals := append([]interface{}{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidInterface,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidFloat64(provided float64, allowed float64, allowedVals ...float64) error {
allAllowedVals := append([]float64{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidFloat64,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidFloat32(provided float32, allowed float32, allowedVals ...float32) error {
allAllowedVals := append([]float32{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidFloat32,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidInt64(provided int64, allowed int64, allowedVals ...int64) error {
allAllowedVals := append([]int64{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidInt64,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidInt32(provided int32, allowed int32, allowedVals ...int32) error {
allAllowedVals := append([]int32{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidInt32,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidInt(provided int, allowed int, allowedVals ...int) error {
allAllowedVals := append([]int{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidInt,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorInvalidStr(provided string, allowed string, allowedVals ...string) error {
allAllowedVals := append([]string{allowed}, allowedVals...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidStr,
Message: fmt.Sprintf("invalid value (got %s, must be %s)", s.UserStr(provided), s.UserStrsOr(allAllowedVals)),
})
}
func ErrorDisallowedValue(provided interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrDisallowedValue,
Message: fmt.Sprintf("%s is not allowed, please use a different value", s.UserStr(provided)),
})
}
func ErrorMustBeLessThanOrEqualTo(provided interface{}, boundary interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrMustBeLessThanOrEqualTo,
Message: fmt.Sprintf("must be less than or equal to %s (got %s)", s.UserStr(boundary), s.UserStr(provided)),
})
}
func ErrorMustBeLessThan(provided interface{}, boundary interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrMustBeLessThan,
Message: fmt.Sprintf("must be less than %s (got %s)", s.UserStr(boundary), s.UserStr(provided)),
})
}
func ErrorMustBeGreaterThanOrEqualTo(provided interface{}, boundary interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrMustBeGreaterThanOrEqualTo,
Message: fmt.Sprintf("must be greater than or equal to %s (got %s)", s.UserStr(boundary), s.UserStr(provided)),
})
}
func ErrorMustBeGreaterThan(provided interface{}, boundary interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrMustBeGreaterThan,
Message: fmt.Sprintf("must be greater than %s (got %s)", s.UserStr(boundary), s.UserStr(provided)),
})
}
func ErrorIsNotMultiple(provided interface{}, multiple interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrIsNotMultiple,
Message: fmt.Sprintf("%s is not a multiple of %s", s.UserStr(provided), s.UserStr(multiple)),
})
}
func ErrorNonStringKeyFound(key interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrNonStringKeyFound,
Message: fmt.Sprintf("non string key found: %s", s.ObjFlat(key)),
})
}
func ErrorInvalidPrimitiveType(provided interface{}, allowedType PrimitiveType, allowedTypes ...PrimitiveType) error {
allAllowedTypes := append([]PrimitiveType{allowedType}, allowedTypes...)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidPrimitiveType,
Message: fmt.Sprintf("%s: invalid type (expected %s)", s.UserStr(provided), s.StrsOr(PrimitiveTypes(allAllowedTypes).StringList())),
})
}
func ErrorDuplicatedValue(val interface{}) error {
return errors.WithStack(&errors.Error{
Kind: ErrDuplicatedValue,
Message: fmt.Sprintf("%s is duplicated", s.UserStr(val)),
})
}
func ErrorTooFewElements(minLength int) error {
return errors.WithStack(&errors.Error{
Kind: ErrTooFewElements,
Message: fmt.Sprintf("must contain at least %d elements", minLength),
})
}
func ErrorTooManyElements(maxLength int) error {
return errors.WithStack(&errors.Error{
Kind: ErrTooManyElements,
Message: fmt.Sprintf("must contain at most %d elements", maxLength),
})
}
func ErrorWrongNumberOfElements(invalidLengths []int) error {
invalidElementsStr := "elements"
if len(invalidLengths) == 1 && invalidLengths[0] == 1 {
invalidElementsStr = "element"
}
invalidLengthStrs := make([]string, len(invalidLengths))
for i, length := range invalidLengths {
invalidLengthStrs[i] = s.Int(length)
}
return errors.WithStack(&errors.Error{
Kind: ErrWrongNumberOfElements,
Message: fmt.Sprintf("cannot contain %s %s", s.StrsOr(invalidLengthStrs), invalidElementsStr),
})
}
func ErrorCannotSetStructField() error {
return errors.WithStack(&errors.Error{
Kind: ErrCannotSetStructField,
Message: "unable to set struct field",
})
}
func ErrorCannotBeNull(isRequired bool) error {
msg := "cannot be null"
if !isRequired {
msg = "cannot be null (specify a value, or remove the key to use the default value)"
}
return errors.WithStack(&errors.Error{
Kind: ErrCannotBeNull,
Message: msg,
})
}
func ErrorCannotBeEmptyOrNull(isRequired bool) error {
msg := "cannot be empty or null"
if !isRequired {
msg = "cannot be empty or null (specify a value, or remove the key to use the default value)"
}
return errors.WithStack(&errors.Error{
Kind: ErrCannotBeEmptyOrNull,
Message: msg,
})
}
func ErrorCannotBeEmpty() error {
return errors.WithStack(&errors.Error{
Kind: ErrCannotBeEmpty,
Message: "cannot be empty",
})
}
func ErrorMustBeDefined(validValues ...interface{}) error {
msg := "must be defined"
if len(validValues) > 0 && !reflect.ValueOf(validValues[0]).IsNil() { // reflect is necessary here
msg = fmt.Sprintf("must be defined, and set to %s", s.UserStrsOr(validValues))
}
return errors.WithStack(&errors.Error{
Kind: ErrMustBeDefined,
Message: msg,
})
}
func ErrorMapMustBeDefined(keys ...string) error {
message := "must be defined"
if len(keys) > 0 {
message = fmt.Sprintf("must be defined, and contain the following keys: %s", s.UserStrsAnd(keys))
}
return errors.WithStack(&errors.Error{
Kind: ErrMapMustBeDefined,
Message: message,
})
}
func ErrorMustBeEmpty() error {
return errors.WithStack(&errors.Error{
Kind: ErrMustBeEmpty,
Message: "must be empty",
})
}
func ErrorEmailTooLong() error {
return errors.WithStack(&errors.Error{
Kind: ErrEmailTooLong,
Message: "email address exceeds maximum length",
})
}
func ErrorEmailInvalid() error {
return errors.WithStack(&errors.Error{
Kind: ErrEmailInvalid,
Message: "invalid email address",
})
}
func ErrorCortexResourceOnlyAllowed(invalidStr string) error {
return errors.WithStack(&errors.Error{
Kind: ErrCortexResourceOnlyAllowed,
Message: fmt.Sprintf("%s: only cortex resource references (which start with @) are allowed in this context", invalidStr),
})
}
func ErrorCortexResourceNotAllowed(resourceName string) error {
return errors.WithStack(&errors.Error{
Kind: ErrCortexResourceNotAllowed,
Message: fmt.Sprintf("@%s: cortex resource references (which start with @) are not allowed in this context", resourceName),
})
}
func ErrorImageVersionMismatch(image, tag, cortexVersion string) error {
return errors.WithStack(&errors.Error{
Kind: ErrImageVersionMismatch,
Message: fmt.Sprintf("the specified image (%s) has a tag (%s) which does not match your Cortex version (%s); please update the image tag, remove the image registry path from your configuration file (to use the default value), or update your CLI by following the instructions at https://docs.cortex.dev/install", image, tag, cortexVersion),
})
}