-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
70 lines (55 loc) · 1.36 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
package validator
import (
"fmt"
"strings"
)
type (
// Error is an interface that represents a validation error
Error interface {
// Field returns a validating field.
Field() Field
// Tag returns a validation tag.
Tag() Tag
// Error returns an error message string.
Error() string
}
fieldError struct {
field Field
tag Tag
// err is an internal error.
err error
// customMessage is a custom error message. TODO:
customMessage string
// suppressErrorFieldValue suppress output of field value.
suppressErrorFieldValue bool
}
// Errors represents validation errors
Errors []Error
)
// ToErrors converts an error to the validation Errors.
func ToErrors(err error) (Errors, bool) {
es, ok := err.(Errors)
return es, ok
}
func (e *fieldError) Field() Field {
return e.field
}
func (e *fieldError) Tag() Tag {
return e.tag
}
func (e *fieldError) Error() string {
if e.err != nil {
return fmt.Sprintf("%s: an internal error occurred in '%s': %v", e.field.Name(), e.tag, e.err)
}
if e.suppressErrorFieldValue {
return fmt.Sprintf("%s: The value does validate as '%s'", e.field.Name(), e.tag)
}
return fmt.Sprintf("%s: '%s' does validate as '%s'", e.field.Name(), e.field.ShortString(), e.tag)
}
func (es Errors) Error() string {
var s []string
for _, e := range es {
s = append(s, e.Error())
}
return strings.Join(s, ";")
}