-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_render.go
277 lines (241 loc) · 8.52 KB
/
errors_render.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
package csvlib
import (
"strings"
"github.com/hashicorp/go-multierror"
"github.com/tiendc/gofn"
)
const (
newLine = "\n"
)
type ErrorRenderConfig struct {
// HeaderFormatKey header format string.
// You can use a localization key as the value to force the renderer to translate the key first.
// If the translation fails, the original value is used for next step.
//
// For example, header format key can be:
// - "HEADER_FORMAT_KEY" (a localization key you define in your localization data such as a json file,
// HEADER_FORMAT_KEY = "CSV decoding result: total errors is {{.TotalError}}")
// - "CSV decoding result: total errors is {{.TotalError}}" (direct string)
//
// Supported params:
// {{.TotalRow}} - number of rows in the CSV data
// {{.TotalRowError}} - number of rows have error
// {{.TotalCellError}} - number of cells have error
// {{.TotalError}} - number of errors
//
// Extra params:
// {{.CrLf}} - line break
// {{.Tab}} - tab character
HeaderFormatKey string
// RowFormatKey format string for each row.
// Similar to the header format key, this can be a localization key or a direct string.
//
// For example, row format key can be:
// - "ROW_FORMAT_KEY" (a localization key you define in your localization data such as a json file)
// - "Row {{.Row}} (line {{.Line}}): {{.Error}}" (direct string)
//
// Supported params:
// {{.Row}} - row index (1-based, row 1 can be the header row if present)
// {{.Line}} - line of row in source file (can be -1 if undetected)
// {{.Error}} - error content of the row which is a list of cell errors
RowFormatKey string
// RowSeparator separator to join row error details, normally a row is in a separated line
RowSeparator string
// CellSeparator separator to join cell error details within a row, normally a comma (`,`)
CellSeparator string
// LineBreak custom new line character (default is `\n`)
LineBreak string
// LocalizeCellFields localize cell's fields before rendering the cell error (default is `true`)
LocalizeCellFields bool
// LocalizeCellHeader localize cell header before rendering the cell error (default is `true`)
LocalizeCellHeader bool
// Params custom params user wants to send to the localization (optional)
Params ParameterMap
// LocalizationFunc function to translate message (optional)
LocalizationFunc LocalizationFunc
// CellRenderFunc custom render function for rendering a cell error (optional).
// The func can return ("", false) to skip rendering the cell error, return ("", true) to let the
// renderer continue using its solution, and return ("<str>", true) to override the value.
//
// Supported params:
// {{.Column}} - column index (0-based)
// {{.ColumnHeader}} - column name
// {{.Value}} - cell value
// {{.Error}} - error detail which is result of calling err.Error()
//
// Use cellErr.WithParam() to add more extra params
CellRenderFunc func(*RowErrors, *CellError, ParameterMap) (string, bool)
// CommonErrorRenderFunc renders common error (not RowErrors, CellError) (optional)
CommonErrorRenderFunc func(error, ParameterMap) (string, error)
}
func defaultRenderConfig() *ErrorRenderConfig {
return &ErrorRenderConfig{
HeaderFormatKey: "Error content: TotalRow: {{.TotalRow}}, TotalRowError: {{.TotalRowError}}, " +
"TotalCellError: {{.TotalCellError}}, TotalError: {{.TotalError}}",
RowFormatKey: "Row {{.Row}} (line {{.Line}}): {{.Error}}",
RowSeparator: newLine,
CellSeparator: ", ",
LineBreak: newLine,
LocalizeCellFields: true,
LocalizeCellHeader: true,
}
}
// SimpleRenderer a simple implementation of error renderer which can produce a text message
// for the input errors.
type SimpleRenderer struct {
cfg *ErrorRenderConfig
sourceErr *Errors
transErr error
}
// NewRenderer creates a new SimpleRenderer
func NewRenderer(err *Errors, options ...func(*ErrorRenderConfig)) (*SimpleRenderer, error) {
cfg := defaultRenderConfig()
for _, opt := range options {
opt(cfg)
}
return &SimpleRenderer{cfg: cfg, sourceErr: err}, nil
}
// Render renders Errors object as text.
//
// Sample output:
//
// There are 5 total errors in your CSV file
// Row 20 (line 21): column 2: invalid type (Int), column 4: value (12345) too big
// Row 30 (line 33): column 2: invalid type (Int), column 4: value (12345) too big, column 6: unexpected
// Row 35 (line 38): column 2: invalid type (Int), column 4: value (12345) too big
// Row 40 (line 44): column 2: invalid type (Int), column 4: value (12345) too big
// Row 41 (line 50): invalid number of columns (10)
func (r *SimpleRenderer) Render() (msg string, transErr error, err error) {
cfg := r.cfg
errs := r.sourceErr.Unwrap()
content := make([]string, 0, len(errs)+1)
params := gofn.MapUpdate(ParameterMap{
"CrLf": cfg.LineBreak,
"Tab": "\t",
"TotalRow": r.sourceErr.TotalRow(),
"TotalError": r.sourceErr.TotalError(),
"TotalRowError": r.sourceErr.TotalRowError(),
"TotalCellError": r.sourceErr.TotalCellError(),
}, cfg.Params)
// Header line
if cfg.HeaderFormatKey != "" {
header := r.localizeKeySkipError(cfg.HeaderFormatKey, params)
if header != "" {
content = append(content, header)
}
}
// Body part (simply each RowErrors object is rendered as a line)
for _, err := range errs {
var detail string
if rowErr, ok := err.(*RowErrors); ok { // nolint: errorlint
detail = r.renderRow(rowErr, params)
} else {
detail = r.renderCommonError(err, params)
}
if detail != "" {
content = append(content, detail)
}
}
return strings.Join(content, cfg.RowSeparator), r.transErr, nil
}
func (r *SimpleRenderer) renderRow(rowErr *RowErrors, exparams ParameterMap) string {
cfg := r.cfg
errs := rowErr.Unwrap()
content := make([]string, 0, len(errs))
params := gofn.MapUpdate(ParameterMap{}, exparams)
params["Row"] = rowErr.Row()
params["Line"] = rowErr.Line()
for _, err := range errs {
var detail string
if cellErr, ok := err.(*CellError); ok { // nolint: errorlint
detail = r.renderCell(rowErr, cellErr, params)
} else {
detail = r.renderCommonError(err, params)
}
if detail != "" {
content = append(content, detail)
}
}
if cfg.RowFormatKey != "" {
params["Error"] = strings.Join(content, cfg.CellSeparator)
return r.localizeKeySkipError(cfg.RowFormatKey, params)
}
return ""
}
func (r *SimpleRenderer) renderCell(rowErr *RowErrors, cellErr *CellError, exparams ParameterMap) string {
params := gofn.MapUpdate(ParameterMap{}, exparams)
params = gofn.MapUpdate(params, r.renderCellFields(cellErr, params))
params["Column"] = cellErr.Column()
params["ColumnHeader"] = r.renderCellHeader(cellErr, params)
params["Value"] = cellErr.Value()
params["Error"] = cellErr.Error()
if r.cfg.CellRenderFunc != nil {
msg, flag := r.cfg.CellRenderFunc(rowErr, cellErr, exparams)
if !flag {
return ""
}
if msg != "" {
return msg
}
}
locKey := cellErr.LocalizationKey()
if locKey == "" {
locKey = cellErr.Error()
}
return r.localizeKeySkipError(locKey, params)
}
func (r *SimpleRenderer) renderCellFields(cellErr *CellError, params ParameterMap) ParameterMap {
if !r.cfg.LocalizeCellFields {
return cellErr.fields
}
result := make(ParameterMap, len(cellErr.fields))
for k, v := range cellErr.fields {
vAsStr, ok := v.(string)
if !ok {
result[k] = v
continue
}
if translated, err := r.localizeKey(vAsStr, params); err != nil {
result[k] = v
} else {
result[k] = translated
}
}
return result
}
func (r *SimpleRenderer) renderCellHeader(cellErr *CellError, params ParameterMap) string {
if !r.cfg.LocalizeCellHeader {
return cellErr.Header()
}
return r.localizeKeySkipError(cellErr.Header(), params)
}
func (r *SimpleRenderer) renderCommonError(err error, params ParameterMap) string {
if r.cfg.CommonErrorRenderFunc == nil {
return r.localizeKeySkipError(err.Error(), params)
}
msg, err := r.cfg.CommonErrorRenderFunc(err, params)
if err != nil {
r.transErr = multierror.Append(r.transErr, err)
}
return msg
}
func (r *SimpleRenderer) localizeKey(key string, params ParameterMap) (string, error) {
if r.cfg.LocalizationFunc == nil {
return processTemplate(key, params)
}
msg, err := r.cfg.LocalizationFunc(key, params)
if err != nil {
err = multierror.Append(ErrLocalization, err)
r.transErr = multierror.Append(r.transErr, err)
return "", err
}
return msg, nil
}
func (r *SimpleRenderer) localizeKeySkipError(key string, params ParameterMap) string {
s, err := r.localizeKey(key, params)
if err == nil || r.cfg.LocalizationFunc == nil {
return s
}
s, _ = processTemplate(key, params)
return s
}