-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_formatter.go
381 lines (346 loc) · 10.6 KB
/
error_formatter.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
package validation
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"text/template"
"github.com/hashicorp/go-multierror"
"github.com/iancoleman/strcase"
"github.com/tiendc/gofn"
)
type (
// ErrorParamFormatter interface represents a formatter of error params
ErrorParamFormatter interface {
Format(k string, v any) string
}
errorParamFormatter struct {
k string
v any
formatter ErrorParamFormatter
}
// FormatFunc type of format function which produces string from input value
FormatFunc func(reflect.Value) string
// TypedParamFormatter interface of param formatter which consists of a set of format functions
// for common Go value types.
TypedParamFormatter interface {
ErrorParamFormatter
// SetNumFormatFunc set format function for numbers
SetNumFormatFunc(FormatFunc)
// SetStrFormatFunc set format function for strings
SetStrFormatFunc(FormatFunc)
// SetBoolFormatFunc set format function for bools
SetBoolFormatFunc(FormatFunc)
// SetSliceFormatFunc set format function for slices
SetSliceFormatFunc(FormatFunc)
// SetMapFormatFunc set format function for maps
SetMapFormatFunc(FormatFunc)
// SetStructFormatFunc set format function for structs
SetStructFormatFunc(FormatFunc)
// SetPtrFormatFunc set format function for pointers
SetPtrFormatFunc(FormatFunc)
// SetCustomFormatFunc set custom format function
SetCustomFormatFunc(FormatFunc)
}
// typedParamFormatter default implementation of TypedParamFormatter
typedParamFormatter struct {
numFormatFunc FormatFunc
strFormatFunc FormatFunc
boolFormatFunc FormatFunc
sliceFormatFunc FormatFunc
mapFormatFunc FormatFunc
structFormatFunc FormatFunc
ptrFormatFunc FormatFunc
customFormatFunc FormatFunc
}
)
func (f *errorParamFormatter) String() string {
if f.formatter != nil {
return f.formatter.Format(f.k, f.v)
}
return fmt.Sprintf("%v", f.v)
}
func (f *typedParamFormatter) SetNumFormatFunc(fn FormatFunc) {
f.numFormatFunc = fn
}
func (f *typedParamFormatter) SetStrFormatFunc(fn FormatFunc) {
f.strFormatFunc = fn
}
func (f *typedParamFormatter) SetBoolFormatFunc(fn FormatFunc) {
f.boolFormatFunc = fn
}
func (f *typedParamFormatter) SetSliceFormatFunc(fn FormatFunc) {
f.sliceFormatFunc = fn
}
func (f *typedParamFormatter) SetMapFormatFunc(fn FormatFunc) {
f.mapFormatFunc = fn
}
func (f *typedParamFormatter) SetStructFormatFunc(fn FormatFunc) {
f.structFormatFunc = fn
}
func (f *typedParamFormatter) SetPtrFormatFunc(fn FormatFunc) {
f.ptrFormatFunc = fn
}
func (f *typedParamFormatter) SetCustomFormatFunc(fn FormatFunc) {
f.customFormatFunc = fn
}
func (f *typedParamFormatter) Format(_ string, v any) string {
if f.numFormatFunc == nil && f.strFormatFunc == nil && f.boolFormatFunc == nil &&
f.sliceFormatFunc == nil && f.mapFormatFunc == nil && f.structFormatFunc == nil &&
f.ptrFormatFunc == nil && f.customFormatFunc == nil {
return fmt.Sprintf("%v", v)
}
return f.format(reflect.ValueOf(v))
}
func (f *typedParamFormatter) format(v reflect.Value) string {
if !v.IsValid() {
return f.customFormat(v)
}
// nolint: exhaustive
switch v.Kind() {
case reflect.String:
return f.strFormat(v)
case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64, reflect.Float32, reflect.Float64,
reflect.Int32, reflect.Uint32, reflect.Int16, reflect.Uint16, reflect.Int8, reflect.Uint8:
return f.numFormat(v)
case reflect.Bool:
return f.boolFormat(v)
case reflect.Slice, reflect.Array:
return f.sliceFormat(v)
case reflect.Map:
return f.mapFormat(v)
case reflect.Struct:
return f.structFormat(v)
case reflect.Pointer:
return f.ptrFormat(v)
default:
return f.customFormat(v)
}
}
func (f *typedParamFormatter) strFormat(v reflect.Value) string {
if f.strFormatFunc == nil {
return f.customFormat(v)
}
return f.strFormatFunc(v)
}
func (f *typedParamFormatter) numFormat(v reflect.Value) string {
if f.numFormatFunc == nil {
return f.customFormat(v)
}
return f.numFormatFunc(v)
}
func (f *typedParamFormatter) boolFormat(v reflect.Value) string {
if f.boolFormatFunc == nil {
return f.customFormat(v)
}
return f.boolFormatFunc(v)
}
func (f *typedParamFormatter) sliceFormat(v reflect.Value) string {
if f.sliceFormatFunc == nil {
return f.customFormat(v)
}
return f.sliceFormatFunc(v)
}
func (f *typedParamFormatter) mapFormat(v reflect.Value) string {
if f.mapFormatFunc == nil {
return f.customFormat(v)
}
return f.mapFormatFunc(v)
}
func (f *typedParamFormatter) structFormat(v reflect.Value) string {
if f.structFormatFunc == nil {
return f.customFormat(v)
}
return f.structFormatFunc(v)
}
func (f *typedParamFormatter) ptrFormat(v reflect.Value) string {
if f.ptrFormatFunc == nil {
return f.format(v.Elem())
}
return f.ptrFormatFunc(v)
}
func (f *typedParamFormatter) customFormat(v reflect.Value) string {
if f.customFormatFunc == nil {
if !v.IsValid() {
return "nil"
}
return fmt.Sprintf("%v", v.Interface())
}
return f.customFormatFunc(v)
}
// NewTypedParamFormatter creates a new TypedParamFormatter with using default format functions
func NewTypedParamFormatter() TypedParamFormatter {
return &typedParamFormatter{}
}
// NewDecimalNumFormatFunc returns a FormatFunc which groups digits of decimal.
//
// For example: '12345' -> '12,345', '12345.6789' -> '12,345.6789'
// To attach this formatter to Error object:
// - err.TypedParamFormatter().SetNumFormatFunc(NewDecimalNumFormatFunc())
// - err.TypedParamFormatter().SetNumFormatFunc(NewDecimalNumFormatFunc("%.5f"))
//
// Deprecated: use NewDecimalFormatFunc instead
func NewDecimalNumFormatFunc(floatFmt ...string) FormatFunc {
return func(v reflect.Value) string {
var s string
// nolint: exhaustive
switch v.Kind() {
case reflect.Float64, reflect.Float32:
fmtStr := "%f"
if len(floatFmt) > 0 {
fmtStr = floatFmt[len(floatFmt)-1]
}
s = fmt.Sprintf(fmtStr, v.Interface())
default:
s = fmt.Sprintf("%v", v.Interface())
}
return gofn.NumberFmtGroup(s, '.', ',')
}
}
// NewDecimalFormatFunc returns a FormatFunc which can format and group digits of decimal or integer.
//
// For example: '12345' -> '12,345', '12345.6789' -> '12,345.6789'
// To attach this formatter to Error object:
// - err.TypedParamFormatter().SetNumFormatFunc(NewDecimalFormatFunc('.', ',', "%.2f"))
func NewDecimalFormatFunc(fractionSep, groupSep byte, floatFmt string) FormatFunc {
return func(v reflect.Value) string {
var s string
// nolint: exhaustive
switch v.Kind() {
case reflect.Float64, reflect.Float32:
fmtStr := floatFmt
if fmtStr == "" {
fmtStr = "%f"
}
s = fmt.Sprintf(fmtStr, v.Interface())
default:
s = fmt.Sprintf("%v", v.Interface())
}
return gofn.NumberFmtGroup(s, fractionSep, groupSep)
}
}
// NewSliceFormatFunc create a new func for formatting a slice.
// Sample arguments: leftWrap "[", rightWrap "]", elemSep ", "
func NewSliceFormatFunc(
elemFormatFunc FormatFunc,
leftWrap, rightWrap string, elemSep string,
) FormatFunc {
return func(v reflect.Value) string {
var sb strings.Builder
sb.WriteString(leftWrap)
for i := 0; i < v.Len(); i++ {
if i != 0 {
sb.WriteString(elemSep)
}
sb.WriteString(elemFormatFunc(v.Index(i)))
}
sb.WriteString(rightWrap)
return sb.String()
}
}
// NewMapFormatFunc create a new func for formatting a map.
// Sample arguments: leftWrap "{", rightWrap "}", kvSep ":", elemSep ", "
func NewMapFormatFunc(
keyFormatFunc, valueFormatFunc FormatFunc,
leftWrap, rightWrap string, kvSep, entrySep string,
) FormatFunc {
return func(v reflect.Value) string {
var sb strings.Builder
sb.WriteString(leftWrap)
iter := v.MapRange()
isFirstItem := true
for iter.Next() {
if isFirstItem {
isFirstItem = false
} else {
sb.WriteString(entrySep)
}
sb.WriteString(keyFormatFunc(iter.Key()))
sb.WriteString(kvSep)
sb.WriteString(valueFormatFunc(iter.Value()))
}
sb.WriteString(rightWrap)
return sb.String()
}
}
// NewJSONFormatFunc create a format func to format input as JSON output
func NewJSONFormatFunc() FormatFunc {
return func(v reflect.Value) string {
s, err := json.Marshal(v.Interface())
if err != nil {
panic(err)
}
return string(s)
}
}
// errorBuildDetail builds detail string of error using the error template string.
// In case error happens, this function still returns the result string before error happens
func errorBuildDetail(e Error) (detail string, retErr error) {
detail = e.Template()
t, err := template.New("error").Parse(detail)
if err != nil {
retErr = multierror.Append(retErr, err)
return
}
params, err := errorBuildParams(e, e.ParamFormatter())
if err != nil {
retErr = multierror.Append(retErr, err)
}
buf := bytes.NewBuffer(make([]byte, 0, 100)) //nolint:mnd
err = t.Execute(buf, params)
if err != nil {
retErr = multierror.Append(retErr, err)
} else {
detail = buf.String()
}
return
}
// errorBuildParams builds params of error with inner errors' params handled
func errorBuildParams(e Error, formatter ErrorParamFormatter) (params ErrorParams, err error) {
params = make(ErrorParams, 10) //nolint:mnd
// If there are inner errors, collect all params of them
for _, inErr := range e.UnwrapAsErrors() {
prefix := strcase.ToCamel(inErr.Type())
if prefix != "" {
prefix += "_"
}
pErr := singleErrorBuildParams(inErr, inErr.ParamFormatter(), prefix, params)
if pErr != nil {
err = multierror.Append(err, pErr)
}
}
// Build params for the current error
pErr := singleErrorBuildParams(e, formatter, "", params)
if pErr != nil {
err = multierror.Append(err, pErr)
}
return params, err
}
// singleErrorBuildParams build params for the specific error
func singleErrorBuildParams(e Error, formatter ErrorParamFormatter, prefix string, params ErrorParams) (err error) {
key := prefix + "Type"
params[key] = &errorParamFormatter{k: key, v: e.Type(), formatter: formatter}
key = prefix + "Value"
params[key] = &errorParamFormatter{k: key, v: e.Value(), formatter: formatter}
key = prefix + "ValueType"
params[key] = &errorParamFormatter{k: key, v: e.ValueType(), formatter: formatter}
field := e.Field()
if field != nil {
key = prefix + "Field"
params[key] = &errorParamFormatter{k: key, v: field.Name, formatter: formatter}
key = prefix + "FieldPath"
params[key] = &errorParamFormatter{k: key, v: field.PathString(false, "."), formatter: formatter}
} else {
err = multierror.Append(err, ErrFieldMissing)
key = prefix + "Field"
params[key] = &errorParamFormatter{k: key, v: "", formatter: formatter}
key = prefix + "FieldPath"
params[key] = &errorParamFormatter{k: key, v: "", formatter: formatter}
}
for k, v := range e.Params() {
key = prefix + k
params[key] = &errorParamFormatter{k: key, v: v, formatter: formatter}
}
return err
}