-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
384 lines (321 loc) · 9.17 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
package validation
import (
"errors"
"strings"
"github.com/iancoleman/strcase"
"github.com/tiendc/gofn"
)
// Errors can be returned from the lib
var (
ErrTypeUnsupported = errors.New("type unsupported")
ErrFieldMissing = errors.New("field missing")
)
const (
rootField = "(root)"
)
type (
// ErrorParams is a map of params specific to each error
ErrorParams map[string]any
// Field represents a validating field
Field struct {
Name string
Parent *Field
}
// Error is the interface for all validation errors in this lib
Error interface {
// Type gets type of error
Type() string
// SetType sets type of error
SetType(string) Error
// Field gets validating field
Field() *Field
// SetField sets validating field
SetField(*Field) Error
// Value gets validating value
Value() any
// SetValue gets validating value
SetValue(any) Error
// ValueType gets type of validating value
ValueType() string
// SetValueType sets type of validating value
SetValueType(string) Error
// Template gets template used to generating error message
Template() string
// SetTemplate sets template of error
SetTemplate(string) Error
// Params gets params of error
Params() ErrorParams
// SetParam sets params of error
SetParam(k string, v any) Error
// ParamFormatter formatter is used to format the error params
// By default it is TypedParamFormatter
ParamFormatter() ErrorParamFormatter
// TypedParamFormatter get TypedParamFormatter attached to the error
// This will return nil when the attached formatter is not a TypedParamFormatter
TypedParamFormatter() TypedParamFormatter
// SetParamFormatter sets params formatter of error
SetParamFormatter(ErrorParamFormatter) Error
// CustomKey gets custom key of error
CustomKey() any
// SetCustomKey sets custom key of error
SetCustomKey(any) Error
// BuildDetail builds error detailed message
BuildDetail() (string, error)
// ParamsWithFormatter gets params with wrapping by the formatter of the error
ParamsWithFormatter() ErrorParams
// String implements fmt.Stringer interface
// This function calls BuildDetail() without raising error
// Should use BuildDetail() for more controls on error
String() string
// Error implement error interface
// See String() string
Error() string
// Unwrap implements errors.Unwrap
Unwrap() []error
// UnwrapAsErrors unwraps the error as `Errors` type
UnwrapAsErrors() Errors
}
// Errors slice type for `Error` objects
Errors []Error
// ErrorMod function used to modify an `Error` object
ErrorMod func(Error)
// errorImpl implementation of Error type
// nolint: errname
errorImpl struct {
errorType string
field *Field
value any
valueType string
template string
params ErrorParams
paramsFormatter ErrorParamFormatter
customKey any
wrappedErrors []Error
}
)
// NewField creates a new Field object
func NewField(name string, parent *Field) *Field {
return &Field{name, parent}
}
func (c *Field) Path(skipRoot bool) []string {
path := []string{c.Name}
t := c.Parent
for t != nil {
path = append(path, t.Name)
t = t.Parent
}
if !skipRoot {
path = append(path, rootField)
}
return gofn.Reverse(path)
}
func (c *Field) PathString(skipRoot bool, sep string) string {
return strings.Join(c.Path(skipRoot), sep)
}
// NewError creates a new Error object
func NewError() Error {
return &errorImpl{
params: ErrorParams{},
paramsFormatter: NewTypedParamFormatter(),
}
}
func (e *errorImpl) Type() string {
return e.errorType
}
func (e *errorImpl) SetType(errorType string) Error {
e.errorType = errorType
return e
}
func (e *errorImpl) Field() *Field {
return e.field
}
func (e *errorImpl) SetField(field *Field) Error {
e.field = field
return e
}
func (e *errorImpl) Value() any {
return e.value
}
func (e *errorImpl) SetValue(value any) Error {
e.value = value
return e
}
func (e *errorImpl) ValueType() string {
return e.valueType
}
func (e *errorImpl) SetValueType(valueType string) Error {
e.valueType = valueType
return e
}
func (e *errorImpl) Template() string {
return e.template
}
func (e *errorImpl) SetTemplate(template string) Error {
e.template = template
return e
}
func (e *errorImpl) Params() ErrorParams {
params := gofn.MapUpdate(make(ErrorParams, len(e.params)), e.params)
// Collect all inner errors' params
for _, inErr := range e.wrappedErrors {
prefix := strcase.ToCamel(inErr.Type())
if prefix != "" {
prefix += "_"
}
for k, v := range inErr.Params() {
params[prefix+k] = v
}
}
return params
}
func (e *errorImpl) SetParam(key string, val any) Error {
if e.params == nil {
e.params = ErrorParams{}
}
e.params[key] = val
return e
}
func (e *errorImpl) ParamFormatter() ErrorParamFormatter {
return e.paramsFormatter
}
func (e *errorImpl) TypedParamFormatter() TypedParamFormatter {
if e.paramsFormatter == nil {
return nil
}
typedFmt, _ := e.paramsFormatter.(TypedParamFormatter)
return typedFmt
}
func (e *errorImpl) SetParamFormatter(formatter ErrorParamFormatter) Error {
e.paramsFormatter = formatter
return e
}
func (e *errorImpl) CustomKey() any {
return e.customKey
}
func (e *errorImpl) SetCustomKey(key any) Error {
e.customKey = key
return e
}
func (e *errorImpl) BuildDetail() (string, error) {
return errorBuildDetail(e)
}
func (e *errorImpl) ParamsWithFormatter() ErrorParams {
params, _ := errorBuildParams(e, e.paramsFormatter)
return params
}
func (e *errorImpl) String() string {
str, _ := errorBuildDetail(e)
return str
}
func (e *errorImpl) Error() string {
return e.String()
}
func (e *errorImpl) Unwrap() []error {
errs := make([]error, 0, len(e.wrappedErrors))
for _, err := range e.wrappedErrors {
errs = append(errs, err)
}
return errs
}
func (e *errorImpl) UnwrapAsErrors() Errors {
return e.wrappedErrors
}
func (e Errors) Error() string {
if len(e) == 0 {
return ""
}
var sb strings.Builder
for i, err := range e {
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(err.Error())
}
return sb.String()
}
// SetField returns a ErrorMod function to set field of error
func SetField(name string, parent *Field) ErrorMod {
return func(err Error) {
_ = err.SetField(NewField(name, parent))
}
}
// SetCustomKey returns a ErrorMod function to set custom key of error
func SetCustomKey(key any) ErrorMod {
return func(err Error) {
_ = err.SetCustomKey(key)
}
}
// SetTemplate returns a ErrorMod function to set template of error
func SetTemplate(template string) ErrorMod {
return func(err Error) {
_ = err.SetTemplate(template)
}
}
// SetParam returns a ErrorMod function to set a param of error
func SetParam(key string, val any) ErrorMod {
return func(err Error) {
_ = err.SetParam(key, val)
}
}
// SetParamFormatter returns a ErrorMod function to set params formatter of error
func SetParamFormatter(formatter ErrorParamFormatter) ErrorMod {
return func(err Error) {
_ = err.SetParamFormatter(formatter)
}
}
// SetNumParamFormatter returns a ErrorMod function to set format function for numbers
func SetNumParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetNumFormatFunc(formatFunc)
}
}
// SetStrParamFormatter returns a ErrorMod function to set format function for strings
func SetStrParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetStrFormatFunc(formatFunc)
}
}
// SetBoolParamFormatter returns a ErrorMod function to set format function for bools
func SetBoolParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetBoolFormatFunc(formatFunc)
}
}
// SetSliceParamFormatter returns a ErrorMod function to set format function for slices
func SetSliceParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetSliceFormatFunc(formatFunc)
}
}
// SetMapParamFormatter returns a ErrorMod function to set format function for maps
func SetMapParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetMapFormatFunc(formatFunc)
}
}
// SetStructParamFormatter returns a ErrorMod function to set format function for structs
func SetStructParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetStructFormatFunc(formatFunc)
}
}
// SetPtrParamFormatter returns a ErrorMod function to set format function for pointers
func SetPtrParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetPtrFormatFunc(formatFunc)
}
}
// SetCustomParamFormatter returns a ErrorMod function to set custom format function
func SetCustomParamFormatter(formatFunc FormatFunc) ErrorMod {
return func(err Error) {
getTypedParamFormatterOrPanic(err).SetCustomFormatFunc(formatFunc)
}
}
// getTypedParamFormatterOrPanic returns the TypedParamFormatter associated with the error or panic if unset
func getTypedParamFormatterOrPanic(err Error) TypedParamFormatter {
formatter := err.TypedParamFormatter()
if formatter == nil {
panic("error does not have a TypedParamFormatter attached")
}
return formatter
}