-
Notifications
You must be signed in to change notification settings - Fork 12
/
error.go
72 lines (59 loc) · 1.54 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
package configuro
import (
"fmt"
"go.uber.org/multierr"
"gopkg.in/go-playground/validator.v9"
)
//ErrValidationErrors Error that hold multiple errors.
type ErrValidationErrors struct {
error
}
//ErrValidationTag Error if validation failed by a tag
type ErrValidationTag struct {
field string
message string
err error
tag string
}
//ErrValidationFunc Error if validation failed by Validatable Interface.
type ErrValidationFunc struct {
field string
err error
}
func newErrFieldTagValidation(field validator.FieldError, message string) *ErrValidationTag {
return &ErrValidationTag{
field: field.Namespace(),
message: message,
err: field.(error),
tag: field.Tag(),
}
}
func newErrValidate(field string, err error) *ErrValidationFunc {
//TODO Get field name for extra context (need to update the recursive validate function to supply the path of the err)
return &ErrValidationFunc{
field: field,
err: err,
}
}
func (e *ErrValidationTag) Error() string {
return fmt.Sprintf(`%s: %s`, e.field, e.message)
}
func (e *ErrValidationFunc) Error() string {
return fmt.Sprintf(`%s`, e.err)
}
//Errors Return a list of Errors held inside ErrValidationErrors.
func (e *ErrValidationErrors) Errors() []error {
return multierr.Errors(e.error)
}
//Unwrap to support errors IS|AS
func (e *ErrValidationTag) Unwrap() error {
return e.err
}
//Unwrap to support errors IS|AS
func (e *ErrValidationFunc) Unwrap() error {
return e.err
}
//Unwrap to support errors IS|AS
func (e *ErrValidationErrors) Unwrap() error {
return e.error
}