-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
156 lines (134 loc) · 3.76 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
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
package valis
import (
"bytes"
"fmt"
"github.com/soranoba/valis/translations"
"golang.org/x/text/language"
"golang.org/x/text/message"
"sync"
)
type (
// ValidationError is an error returned by Validator.Validate by default.
ValidationError struct {
errors []*LocationError
nameResolver LocationNameResolver
}
Error = *errorDetail
LocationError struct {
Error
Location *Location
}
// ErrorCollector is an interface that receives some Error of each rule and creates the error returned by Validator.Validate.
ErrorCollector interface {
HasError() bool
Add(loc *Location, err Error)
MakeError() error
}
ErrorCollectorFactoryFunc func() ErrorCollector
)
type (
standardErrorCollector struct {
nameResolver LocationNameResolver
errors []*LocationError
}
errorDetail struct {
code string
params []interface{}
value interface{}
valueBeforeConversion interface{}
}
)
var (
enCatalogOnce sync.Once
enCatalog *translations.Catalog
)
// NewValidationError returns a new ValidationError.
func NewValidationError(nameResolver LocationNameResolver, errors []*LocationError) *ValidationError {
return &ValidationError{errors: errors, nameResolver: nameResolver}
}
func (e *ValidationError) Details() []*LocationError {
return e.errors
}
func (e *ValidationError) Translate(p *message.Printer) map[string][]string {
trans := make(map[string][]string)
for _, locErr := range e.errors {
key := e.nameResolver.ResolveLocationName(locErr.Location)
trans[key] = append(trans[key], p.Sprintf(locErr.Error.Code(), locErr.Error.Params()...))
}
return trans
}
func (e *ValidationError) Error() string {
enCatalogOnce.Do(func() {
enCatalog = translations.NewCatalog()
enCatalog.Set(translations.DefaultEnglish)
})
p := message.NewPrinter(language.English, message.Catalog(enCatalog))
buf := bytes.NewBuffer(nil)
for i, locErr := range e.errors {
name := e.nameResolver.ResolveLocationName(locErr.Location)
buf.WriteString(fmt.Sprintf("(%s) ", locErr.Code()))
if name != "" {
buf.WriteString(name)
buf.WriteString(" ")
}
buf.WriteString(p.Sprintf(locErr.Error.Code(), locErr.Error.Params()...))
if len(e.errors)-1 != i {
buf.WriteString("\n")
}
}
return buf.String()
}
// NewStandardErrorCollector returns an ErrorCollector used by default.
func NewStandardErrorCollector(nameResolver LocationNameResolver) ErrorCollector {
return &standardErrorCollector{
errors: make([]*LocationError, 0),
nameResolver: nameResolver,
}
}
func (c *standardErrorCollector) HasError() bool {
return len(c.errors) > 0
}
func (c *standardErrorCollector) Add(loc *Location, err Error) {
c.errors = append(c.errors, &LocationError{
Location: loc,
Error: err,
})
}
func (c *standardErrorCollector) MakeError() error {
if c.HasError() {
return NewValidationError(c.nameResolver, c.errors)
}
return nil
}
func NewError(code string, value interface{}, params ...interface{}) Error {
return &errorDetail{
code: code,
params: params,
value: value,
}
}
// Code returns an error code.
// See also code sub-package.
func (e *errorDetail) Code() string {
return e.code
}
// Params returns translation parameters.
// See translations sub-package for details.
func (e *errorDetail) Params() []interface{} {
return e.params
}
// Value returns a value received by Rule that created the error.
func (e *errorDetail) Value() interface{} {
return e.value
}
// ValueBeforeConversion returns the original value at that location.
// If To is not used, the same value as Value is returned.
func (e *errorDetail) ValueBeforeConversion() interface{} {
if e.valueBeforeConversion == nil {
return e.value
}
return e.valueBeforeConversion
}
func (e *errorDetail) Error() string {
return e.code
}