podman

Форк
0
611 строк · 16.1 Кб
1
// Copyright 2015 go-swagger maintainers
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package errors
16

17
import (
18
	"encoding/json"
19
	"fmt"
20
	"strings"
21
)
22

23
const (
24
	invalidType               = "%s is an invalid type name"
25
	typeFail                  = "%s in %s must be of type %s"
26
	typeFailWithData          = "%s in %s must be of type %s: %q"
27
	typeFailWithError         = "%s in %s must be of type %s, because: %s"
28
	requiredFail              = "%s in %s is required"
29
	readOnlyFail              = "%s in %s is readOnly"
30
	tooLongMessage            = "%s in %s should be at most %d chars long"
31
	tooShortMessage           = "%s in %s should be at least %d chars long"
32
	patternFail               = "%s in %s should match '%s'"
33
	enumFail                  = "%s in %s should be one of %v"
34
	multipleOfFail            = "%s in %s should be a multiple of %v"
35
	maxIncFail                = "%s in %s should be less than or equal to %v"
36
	maxExcFail                = "%s in %s should be less than %v"
37
	minIncFail                = "%s in %s should be greater than or equal to %v"
38
	minExcFail                = "%s in %s should be greater than %v"
39
	uniqueFail                = "%s in %s shouldn't contain duplicates"
40
	maxItemsFail              = "%s in %s should have at most %d items"
41
	minItemsFail              = "%s in %s should have at least %d items"
42
	typeFailNoIn              = "%s must be of type %s"
43
	typeFailWithDataNoIn      = "%s must be of type %s: %q"
44
	typeFailWithErrorNoIn     = "%s must be of type %s, because: %s"
45
	requiredFailNoIn          = "%s is required"
46
	readOnlyFailNoIn          = "%s is readOnly"
47
	tooLongMessageNoIn        = "%s should be at most %d chars long"
48
	tooShortMessageNoIn       = "%s should be at least %d chars long"
49
	patternFailNoIn           = "%s should match '%s'"
50
	enumFailNoIn              = "%s should be one of %v"
51
	multipleOfFailNoIn        = "%s should be a multiple of %v"
52
	maxIncFailNoIn            = "%s should be less than or equal to %v"
53
	maxExcFailNoIn            = "%s should be less than %v"
54
	minIncFailNoIn            = "%s should be greater than or equal to %v"
55
	minExcFailNoIn            = "%s should be greater than %v"
56
	uniqueFailNoIn            = "%s shouldn't contain duplicates"
57
	maxItemsFailNoIn          = "%s should have at most %d items"
58
	minItemsFailNoIn          = "%s should have at least %d items"
59
	noAdditionalItems         = "%s in %s can't have additional items"
60
	noAdditionalItemsNoIn     = "%s can't have additional items"
61
	tooFewProperties          = "%s in %s should have at least %d properties"
62
	tooFewPropertiesNoIn      = "%s should have at least %d properties"
63
	tooManyProperties         = "%s in %s should have at most %d properties"
64
	tooManyPropertiesNoIn     = "%s should have at most %d properties"
65
	unallowedProperty         = "%s.%s in %s is a forbidden property"
66
	unallowedPropertyNoIn     = "%s.%s is a forbidden property"
67
	failedAllPatternProps     = "%s.%s in %s failed all pattern properties"
68
	failedAllPatternPropsNoIn = "%s.%s failed all pattern properties"
69
	multipleOfMustBePositive  = "factor MultipleOf declared for %s must be positive: %v"
70
)
71

72
// All code responses can be used to differentiate errors for different handling
73
// by the consuming program
74
const (
75
	// CompositeErrorCode remains 422 for backwards-compatibility
76
	// and to separate it from validation errors with cause
77
	CompositeErrorCode = 422
78
	// InvalidTypeCode is used for any subclass of invalid types
79
	InvalidTypeCode = 600 + iota
80
	RequiredFailCode
81
	TooLongFailCode
82
	TooShortFailCode
83
	PatternFailCode
84
	EnumFailCode
85
	MultipleOfFailCode
86
	MaxFailCode
87
	MinFailCode
88
	UniqueFailCode
89
	MaxItemsFailCode
90
	MinItemsFailCode
91
	NoAdditionalItemsCode
92
	TooFewPropertiesCode
93
	TooManyPropertiesCode
94
	UnallowedPropertyCode
95
	FailedAllPatternPropsCode
96
	MultipleOfMustBePositiveCode
97
	ReadOnlyFailCode
98
)
99

100
// CompositeError is an error that groups several errors together
101
type CompositeError struct {
102
	Errors  []error
103
	code    int32
104
	message string
105
}
106

107
// Code for this error
108
func (c *CompositeError) Code() int32 {
109
	return c.code
110
}
111

112
func (c *CompositeError) Error() string {
113
	if len(c.Errors) > 0 {
114
		msgs := []string{c.message + ":"}
115
		for _, e := range c.Errors {
116
			msgs = append(msgs, e.Error())
117
		}
118
		return strings.Join(msgs, "\n")
119
	}
120
	return c.message
121
}
122

123
// MarshalJSON implements the JSON encoding interface
124
func (c CompositeError) MarshalJSON() ([]byte, error) {
125
	return json.Marshal(map[string]interface{}{
126
		"code":    c.code,
127
		"message": c.message,
128
		"errors":  c.Errors,
129
	})
130
}
131

132
// CompositeValidationError an error to wrap a bunch of other errors
133
func CompositeValidationError(errors ...error) *CompositeError {
134
	return &CompositeError{
135
		code:    CompositeErrorCode,
136
		Errors:  append([]error{}, errors...),
137
		message: "validation failure list",
138
	}
139
}
140

141
// ValidateName recursively sets the name for all validations or updates them for nested properties
142
func (c *CompositeError) ValidateName(name string) *CompositeError {
143
	for i, e := range c.Errors {
144
		if ve, ok := e.(*Validation); ok {
145
			c.Errors[i] = ve.ValidateName(name)
146
		} else if ce, ok := e.(*CompositeError); ok {
147
			c.Errors[i] = ce.ValidateName(name)
148
		}
149
	}
150

151
	return c
152
}
153

154
// FailedAllPatternProperties an error for when the property doesn't match a pattern
155
func FailedAllPatternProperties(name, in, key string) *Validation {
156
	msg := fmt.Sprintf(failedAllPatternProps, name, key, in)
157
	if in == "" {
158
		msg = fmt.Sprintf(failedAllPatternPropsNoIn, name, key)
159
	}
160
	return &Validation{
161
		code:    FailedAllPatternPropsCode,
162
		Name:    name,
163
		In:      in,
164
		Value:   key,
165
		message: msg,
166
	}
167
}
168

169
// PropertyNotAllowed an error for when the property doesn't match a pattern
170
func PropertyNotAllowed(name, in, key string) *Validation {
171
	msg := fmt.Sprintf(unallowedProperty, name, key, in)
172
	if in == "" {
173
		msg = fmt.Sprintf(unallowedPropertyNoIn, name, key)
174
	}
175
	return &Validation{
176
		code:    UnallowedPropertyCode,
177
		Name:    name,
178
		In:      in,
179
		Value:   key,
180
		message: msg,
181
	}
182
}
183

184
// TooFewProperties an error for an object with too few properties
185
func TooFewProperties(name, in string, n int64) *Validation {
186
	msg := fmt.Sprintf(tooFewProperties, name, in, n)
187
	if in == "" {
188
		msg = fmt.Sprintf(tooFewPropertiesNoIn, name, n)
189
	}
190
	return &Validation{
191
		code:    TooFewPropertiesCode,
192
		Name:    name,
193
		In:      in,
194
		Value:   n,
195
		message: msg,
196
	}
197
}
198

199
// TooManyProperties an error for an object with too many properties
200
func TooManyProperties(name, in string, n int64) *Validation {
201
	msg := fmt.Sprintf(tooManyProperties, name, in, n)
202
	if in == "" {
203
		msg = fmt.Sprintf(tooManyPropertiesNoIn, name, n)
204
	}
205
	return &Validation{
206
		code:    TooManyPropertiesCode,
207
		Name:    name,
208
		In:      in,
209
		Value:   n,
210
		message: msg,
211
	}
212
}
213

214
// AdditionalItemsNotAllowed an error for invalid additional items
215
func AdditionalItemsNotAllowed(name, in string) *Validation {
216
	msg := fmt.Sprintf(noAdditionalItems, name, in)
217
	if in == "" {
218
		msg = fmt.Sprintf(noAdditionalItemsNoIn, name)
219
	}
220
	return &Validation{
221
		code:    NoAdditionalItemsCode,
222
		Name:    name,
223
		In:      in,
224
		message: msg,
225
	}
226
}
227

228
// InvalidCollectionFormat another flavor of invalid type error
229
func InvalidCollectionFormat(name, in, format string) *Validation {
230
	return &Validation{
231
		code:    InvalidTypeCode,
232
		Name:    name,
233
		In:      in,
234
		Value:   format,
235
		message: fmt.Sprintf("the collection format %q is not supported for the %s param %q", format, in, name),
236
	}
237
}
238

239
// InvalidTypeName an error for when the type is invalid
240
func InvalidTypeName(typeName string) *Validation {
241
	return &Validation{
242
		code:    InvalidTypeCode,
243
		Value:   typeName,
244
		message: fmt.Sprintf(invalidType, typeName),
245
	}
246
}
247

248
// InvalidType creates an error for when the type is invalid
249
func InvalidType(name, in, typeName string, value interface{}) *Validation {
250
	var message string
251

252
	if in != "" {
253
		switch value.(type) {
254
		case string:
255
			message = fmt.Sprintf(typeFailWithData, name, in, typeName, value)
256
		case error:
257
			message = fmt.Sprintf(typeFailWithError, name, in, typeName, value)
258
		default:
259
			message = fmt.Sprintf(typeFail, name, in, typeName)
260
		}
261
	} else {
262
		switch value.(type) {
263
		case string:
264
			message = fmt.Sprintf(typeFailWithDataNoIn, name, typeName, value)
265
		case error:
266
			message = fmt.Sprintf(typeFailWithErrorNoIn, name, typeName, value)
267
		default:
268
			message = fmt.Sprintf(typeFailNoIn, name, typeName)
269
		}
270
	}
271

272
	return &Validation{
273
		code:    InvalidTypeCode,
274
		Name:    name,
275
		In:      in,
276
		Value:   value,
277
		message: message,
278
	}
279

280
}
281

282
// DuplicateItems error for when an array contains duplicates
283
func DuplicateItems(name, in string) *Validation {
284
	msg := fmt.Sprintf(uniqueFail, name, in)
285
	if in == "" {
286
		msg = fmt.Sprintf(uniqueFailNoIn, name)
287
	}
288
	return &Validation{
289
		code:    UniqueFailCode,
290
		Name:    name,
291
		In:      in,
292
		message: msg,
293
	}
294
}
295

296
// TooManyItems error for when an array contains too many items
297
func TooManyItems(name, in string, max int64, value interface{}) *Validation {
298
	msg := fmt.Sprintf(maxItemsFail, name, in, max)
299
	if in == "" {
300
		msg = fmt.Sprintf(maxItemsFailNoIn, name, max)
301
	}
302

303
	return &Validation{
304
		code:    MaxItemsFailCode,
305
		Name:    name,
306
		In:      in,
307
		Value:   value,
308
		message: msg,
309
	}
310
}
311

312
// TooFewItems error for when an array contains too few items
313
func TooFewItems(name, in string, min int64, value interface{}) *Validation {
314
	msg := fmt.Sprintf(minItemsFail, name, in, min)
315
	if in == "" {
316
		msg = fmt.Sprintf(minItemsFailNoIn, name, min)
317
	}
318
	return &Validation{
319
		code:    MinItemsFailCode,
320
		Name:    name,
321
		In:      in,
322
		Value:   value,
323
		message: msg,
324
	}
325
}
326

327
// ExceedsMaximumInt error for when maximum validation fails
328
func ExceedsMaximumInt(name, in string, max int64, exclusive bool, value interface{}) *Validation {
329
	var message string
330
	if in == "" {
331
		m := maxIncFailNoIn
332
		if exclusive {
333
			m = maxExcFailNoIn
334
		}
335
		message = fmt.Sprintf(m, name, max)
336
	} else {
337
		m := maxIncFail
338
		if exclusive {
339
			m = maxExcFail
340
		}
341
		message = fmt.Sprintf(m, name, in, max)
342
	}
343
	return &Validation{
344
		code:    MaxFailCode,
345
		Name:    name,
346
		In:      in,
347
		Value:   value,
348
		message: message,
349
	}
350
}
351

352
// ExceedsMaximumUint error for when maximum validation fails
353
func ExceedsMaximumUint(name, in string, max uint64, exclusive bool, value interface{}) *Validation {
354
	var message string
355
	if in == "" {
356
		m := maxIncFailNoIn
357
		if exclusive {
358
			m = maxExcFailNoIn
359
		}
360
		message = fmt.Sprintf(m, name, max)
361
	} else {
362
		m := maxIncFail
363
		if exclusive {
364
			m = maxExcFail
365
		}
366
		message = fmt.Sprintf(m, name, in, max)
367
	}
368
	return &Validation{
369
		code:    MaxFailCode,
370
		Name:    name,
371
		In:      in,
372
		Value:   value,
373
		message: message,
374
	}
375
}
376

377
// ExceedsMaximum error for when maximum validation fails
378
func ExceedsMaximum(name, in string, max float64, exclusive bool, value interface{}) *Validation {
379
	var message string
380
	if in == "" {
381
		m := maxIncFailNoIn
382
		if exclusive {
383
			m = maxExcFailNoIn
384
		}
385
		message = fmt.Sprintf(m, name, max)
386
	} else {
387
		m := maxIncFail
388
		if exclusive {
389
			m = maxExcFail
390
		}
391
		message = fmt.Sprintf(m, name, in, max)
392
	}
393
	return &Validation{
394
		code:    MaxFailCode,
395
		Name:    name,
396
		In:      in,
397
		Value:   value,
398
		message: message,
399
	}
400
}
401

402
// ExceedsMinimumInt error for when minimum validation fails
403
func ExceedsMinimumInt(name, in string, min int64, exclusive bool, value interface{}) *Validation {
404
	var message string
405
	if in == "" {
406
		m := minIncFailNoIn
407
		if exclusive {
408
			m = minExcFailNoIn
409
		}
410
		message = fmt.Sprintf(m, name, min)
411
	} else {
412
		m := minIncFail
413
		if exclusive {
414
			m = minExcFail
415
		}
416
		message = fmt.Sprintf(m, name, in, min)
417
	}
418
	return &Validation{
419
		code:    MinFailCode,
420
		Name:    name,
421
		In:      in,
422
		Value:   value,
423
		message: message,
424
	}
425
}
426

427
// ExceedsMinimumUint error for when minimum validation fails
428
func ExceedsMinimumUint(name, in string, min uint64, exclusive bool, value interface{}) *Validation {
429
	var message string
430
	if in == "" {
431
		m := minIncFailNoIn
432
		if exclusive {
433
			m = minExcFailNoIn
434
		}
435
		message = fmt.Sprintf(m, name, min)
436
	} else {
437
		m := minIncFail
438
		if exclusive {
439
			m = minExcFail
440
		}
441
		message = fmt.Sprintf(m, name, in, min)
442
	}
443
	return &Validation{
444
		code:    MinFailCode,
445
		Name:    name,
446
		In:      in,
447
		Value:   value,
448
		message: message,
449
	}
450
}
451

452
// ExceedsMinimum error for when minimum validation fails
453
func ExceedsMinimum(name, in string, min float64, exclusive bool, value interface{}) *Validation {
454
	var message string
455
	if in == "" {
456
		m := minIncFailNoIn
457
		if exclusive {
458
			m = minExcFailNoIn
459
		}
460
		message = fmt.Sprintf(m, name, min)
461
	} else {
462
		m := minIncFail
463
		if exclusive {
464
			m = minExcFail
465
		}
466
		message = fmt.Sprintf(m, name, in, min)
467
	}
468
	return &Validation{
469
		code:    MinFailCode,
470
		Name:    name,
471
		In:      in,
472
		Value:   value,
473
		message: message,
474
	}
475
}
476

477
// NotMultipleOf error for when multiple of validation fails
478
func NotMultipleOf(name, in string, multiple, value interface{}) *Validation {
479
	var msg string
480
	if in == "" {
481
		msg = fmt.Sprintf(multipleOfFailNoIn, name, multiple)
482
	} else {
483
		msg = fmt.Sprintf(multipleOfFail, name, in, multiple)
484
	}
485
	return &Validation{
486
		code:    MultipleOfFailCode,
487
		Name:    name,
488
		In:      in,
489
		Value:   value,
490
		message: msg,
491
	}
492
}
493

494
// EnumFail error for when an enum validation fails
495
func EnumFail(name, in string, value interface{}, values []interface{}) *Validation {
496
	var msg string
497
	if in == "" {
498
		msg = fmt.Sprintf(enumFailNoIn, name, values)
499
	} else {
500
		msg = fmt.Sprintf(enumFail, name, in, values)
501
	}
502

503
	return &Validation{
504
		code:    EnumFailCode,
505
		Name:    name,
506
		In:      in,
507
		Value:   value,
508
		Values:  values,
509
		message: msg,
510
	}
511
}
512

513
// Required error for when a value is missing
514
func Required(name, in string, value interface{}) *Validation {
515
	var msg string
516
	if in == "" {
517
		msg = fmt.Sprintf(requiredFailNoIn, name)
518
	} else {
519
		msg = fmt.Sprintf(requiredFail, name, in)
520
	}
521
	return &Validation{
522
		code:    RequiredFailCode,
523
		Name:    name,
524
		In:      in,
525
		Value:   value,
526
		message: msg,
527
	}
528
}
529

530
// ReadOnly error for when a value is present in request
531
func ReadOnly(name, in string, value interface{}) *Validation {
532
	var msg string
533
	if in == "" {
534
		msg = fmt.Sprintf(readOnlyFailNoIn, name)
535
	} else {
536
		msg = fmt.Sprintf(readOnlyFail, name, in)
537
	}
538
	return &Validation{
539
		code:    ReadOnlyFailCode,
540
		Name:    name,
541
		In:      in,
542
		Value:   value,
543
		message: msg,
544
	}
545
}
546

547
// TooLong error for when a string is too long
548
func TooLong(name, in string, max int64, value interface{}) *Validation {
549
	var msg string
550
	if in == "" {
551
		msg = fmt.Sprintf(tooLongMessageNoIn, name, max)
552
	} else {
553
		msg = fmt.Sprintf(tooLongMessage, name, in, max)
554
	}
555
	return &Validation{
556
		code:    TooLongFailCode,
557
		Name:    name,
558
		In:      in,
559
		Value:   value,
560
		message: msg,
561
	}
562
}
563

564
// TooShort error for when a string is too short
565
func TooShort(name, in string, min int64, value interface{}) *Validation {
566
	var msg string
567
	if in == "" {
568
		msg = fmt.Sprintf(tooShortMessageNoIn, name, min)
569
	} else {
570
		msg = fmt.Sprintf(tooShortMessage, name, in, min)
571
	}
572

573
	return &Validation{
574
		code:    TooShortFailCode,
575
		Name:    name,
576
		In:      in,
577
		Value:   value,
578
		message: msg,
579
	}
580
}
581

582
// FailedPattern error for when a string fails a regex pattern match
583
// the pattern that is returned is the ECMA syntax version of the pattern not the golang version.
584
func FailedPattern(name, in, pattern string, value interface{}) *Validation {
585
	var msg string
586
	if in == "" {
587
		msg = fmt.Sprintf(patternFailNoIn, name, pattern)
588
	} else {
589
		msg = fmt.Sprintf(patternFail, name, in, pattern)
590
	}
591

592
	return &Validation{
593
		code:    PatternFailCode,
594
		Name:    name,
595
		In:      in,
596
		Value:   value,
597
		message: msg,
598
	}
599
}
600

601
// MultipleOfMustBePositive error for when a
602
// multipleOf factor is negative
603
func MultipleOfMustBePositive(name, in string, factor interface{}) *Validation {
604
	return &Validation{
605
		code:    MultipleOfMustBePositiveCode,
606
		Name:    name,
607
		In:      in,
608
		Value:   factor,
609
		message: fmt.Sprintf(multipleOfMustBePositive, name, factor),
610
	}
611
}
612

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.