1
|
|
|
/* |
2
|
|
|
Package e2h_test its the test package of the Enhanced Error Handling module |
3
|
|
|
*/ |
4
|
|
|
package e2h_test |
5
|
|
|
|
6
|
|
|
import ( |
7
|
|
|
"fmt" |
8
|
|
|
"os" |
9
|
|
|
"path/filepath" |
10
|
|
|
"runtime" |
11
|
|
|
"strings" |
12
|
|
|
"testing" |
13
|
|
|
|
14
|
|
|
"github.com/cdleo/go-commons/formatter" |
15
|
|
|
"github.com/cdleo/go-e2h" |
16
|
|
|
e2hformat "github.com/cdleo/go-e2h/formatter" |
17
|
|
|
"github.com/stretchr/testify/require" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
func TestEnhancedError_StdErr_RawFormatter_GetSource(t *testing.T) { |
21
|
|
|
|
22
|
|
|
// Setup |
23
|
|
|
stdErr := fmt.Errorf("This is a standard error") |
24
|
|
|
rawFormatter, err := e2hformat.NewFormatter(e2hformat.Format_Raw) |
25
|
|
|
require.Nil(t, err) |
26
|
|
|
|
27
|
|
|
// Execute |
28
|
|
|
output_raw := rawFormatter.Source(stdErr) |
29
|
|
|
|
30
|
|
|
// Check |
31
|
|
|
require.Equal(t, output_raw, stdErr.Error()) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func TestEnhancedError_StdErr_JSONFormatter_GetSource(t *testing.T) { |
35
|
|
|
|
36
|
|
|
// Setup |
37
|
|
|
stdErr := fmt.Errorf("This is a standard error") |
38
|
|
|
jsonFormatter, err := e2hformat.NewFormatter(e2hformat.Format_JSON) |
39
|
|
|
require.Nil(t, err) |
40
|
|
|
|
41
|
|
|
// Execute |
42
|
|
|
output_json := jsonFormatter.Source(stdErr) |
43
|
|
|
|
44
|
|
|
// Check |
45
|
|
|
require.Equal(t, output_json, "{\"error\":\"This is a standard error\"}") |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
func TestEnhancedError_StdErr_RawFormatter_Format(t *testing.T) { |
49
|
|
|
|
50
|
|
|
// Setup |
51
|
|
|
stdErr := fmt.Errorf("This is a standard error") |
52
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
53
|
|
|
params := e2hformat.Params{} |
54
|
|
|
|
55
|
|
|
// Execute |
56
|
|
|
output := rawFormatter.Format(stdErr, params) |
57
|
|
|
|
58
|
|
|
// Check |
59
|
|
|
require.Equal(t, output, "This is a standard error") |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
func TestEnhancedError_StdErr_JSONFormatter_Format(t *testing.T) { |
63
|
|
|
|
64
|
|
|
// Setup |
65
|
|
|
stdErr := fmt.Errorf("This is a standard error") |
66
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
67
|
|
|
params := e2hformat.Params{} |
68
|
|
|
|
69
|
|
|
// Execute |
70
|
|
|
outputJSON := jsonFormatter.Format(stdErr, params) |
71
|
|
|
|
72
|
|
|
// Check |
73
|
|
|
require.Equal(t, outputJSON, "{\"error\":\"This is a standard error\",\"stack_trace\":[]}") |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_GetCause(t *testing.T) { |
77
|
|
|
|
78
|
|
|
// Setup |
79
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
80
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
81
|
|
|
|
82
|
|
|
// Execute |
83
|
|
|
output := rawFormatter.Source(enhancedErr) |
84
|
|
|
|
85
|
|
|
// Check |
86
|
|
|
require.Equal(t, output, "This is a standard error [Error wrapped with additional info]") |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_GetCause(t *testing.T) { |
90
|
|
|
|
91
|
|
|
// Setup |
92
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
93
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
94
|
|
|
|
95
|
|
|
// Execute |
96
|
|
|
output := jsonFormatter.Source(enhancedErr) |
97
|
|
|
|
98
|
|
|
// Check |
99
|
|
|
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"context\":\"Error wrapped with additional info\"}") |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_Format(t *testing.T) { |
103
|
|
|
|
104
|
|
|
// Setup |
105
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
106
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
107
|
|
|
_, b, _, _ := runtime.Caller(0) |
108
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
109
|
|
|
params := e2hformat.Params{ |
110
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
111
|
|
|
PathHidingValue: hideThisPath, |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Execute |
115
|
|
|
output := rawFormatter.Format(enhancedErr, params) |
116
|
|
|
|
117
|
|
|
// Check |
118
|
|
|
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format (e2h_test.go:105) [Error wrapped with additional info];") |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_Format(t *testing.T) { |
122
|
|
|
|
123
|
|
|
// Setup |
124
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
125
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
126
|
|
|
_, b, _, _ := runtime.Caller(0) |
127
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
128
|
|
|
params := e2hformat.Params{ |
129
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
130
|
|
|
PathHidingValue: hideThisPath, |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Execute |
134
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
135
|
|
|
|
136
|
|
|
// Check |
137
|
|
|
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format\",\"caller\":\"e2h_test.go:124\",\"context\":\"Error wrapped with additional info\"}]}") |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_Format_Beautified(t *testing.T) { |
141
|
|
|
|
142
|
|
|
// Setup |
143
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
144
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
145
|
|
|
_, b, _, _ := runtime.Caller(0) |
146
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
147
|
|
|
params := e2hformat.Params{ |
148
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
149
|
|
|
PathHidingValue: hideThisPath, |
150
|
|
|
Beautify: true, |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Execute |
154
|
|
|
output := rawFormatter.Format(enhancedErr, params) |
155
|
|
|
|
156
|
|
|
// Check |
157
|
|
|
require.Equal(t, output, "This is a standard error\ngithub.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Beautified (e2h_test.go:143)\n\tError wrapped with additional info") |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified(t *testing.T) { |
161
|
|
|
|
162
|
|
|
// Setup |
163
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
164
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
165
|
|
|
_, b, _, _ := runtime.Caller(0) |
166
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
167
|
|
|
params := e2hformat.Params{ |
168
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
169
|
|
|
PathHidingValue: hideThisPath, |
170
|
|
|
Beautify: true, |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Execute |
174
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
175
|
|
|
|
176
|
|
|
// Check |
177
|
|
|
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified\",\n\t\t\t\"caller\": \"e2h_test.go:163\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t}\n\t]\n}") |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_Format_Inverted(t *testing.T) { |
181
|
|
|
|
182
|
|
|
// Setup |
183
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
184
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
185
|
|
|
_, b, _, _ := runtime.Caller(0) |
186
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
187
|
|
|
params := e2hformat.Params{ |
188
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
189
|
|
|
PathHidingValue: hideThisPath, |
190
|
|
|
InvertCallstack: true, |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Execute |
194
|
|
|
output := rawFormatter.Format(enhancedErr, params) |
195
|
|
|
|
196
|
|
|
// Check |
197
|
|
|
require.Equal(t, output, "github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Inverted (e2h_test.go:183) [Error wrapped with additional info]; This is a standard error;") |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted(t *testing.T) { |
201
|
|
|
|
202
|
|
|
// Setup |
203
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
204
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
205
|
|
|
_, b, _, _ := runtime.Caller(0) |
206
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
207
|
|
|
params := e2hformat.Params{ |
208
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
209
|
|
|
PathHidingValue: hideThisPath, |
210
|
|
|
InvertCallstack: true, |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Execute |
214
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
215
|
|
|
|
216
|
|
|
// Check |
217
|
|
|
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted\",\"caller\":\"e2h_test.go:203\",\"context\":\"Error wrapped with additional info\"}]}") |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden(t *testing.T) { |
221
|
|
|
|
222
|
|
|
// Setup |
223
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
224
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
225
|
|
|
_, b, _, _ := runtime.Caller(0) |
226
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
227
|
|
|
params := e2hformat.Params{ |
228
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
229
|
|
|
PathHidingValue: hideThisPath, |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Execute |
233
|
|
|
output := rawFormatter.Format(enhancedErr, params) |
234
|
|
|
|
235
|
|
|
// Check |
236
|
|
|
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden (e2h_test.go:223) [Error wrapped with additional info];") |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden(t *testing.T) { |
240
|
|
|
|
241
|
|
|
// Setup |
242
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
243
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
244
|
|
|
_, b, _, _ := runtime.Caller(0) |
245
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
246
|
|
|
params := e2hformat.Params{ |
247
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
248
|
|
|
PathHidingValue: hideThisPath, |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// Execute |
252
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
253
|
|
|
|
254
|
|
|
// Check |
255
|
|
|
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden\",\"caller\":\"e2h_test.go:242\",\"context\":\"Error wrapped with additional info\"}]}") |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
func TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden(t *testing.T) { |
259
|
|
|
|
260
|
|
|
// Setup |
261
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
262
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
263
|
|
|
_, b, _, _ := runtime.Caller(0) |
264
|
|
|
path := strings.Split(filepath.Dir(b), string(os.PathSeparator)) |
265
|
|
|
params := e2hformat.Params{ |
266
|
|
|
PathHidingMethod: formatter.HidingMethod_ToFolder, |
267
|
|
|
PathHidingValue: path[len(path)-1], |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// Execute |
271
|
|
|
output := rawFormatter.Format(enhancedErr, params) |
272
|
|
|
|
273
|
|
|
want := strings.ReplaceAll("This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden (<LAST_DIR>/e2h_test.go:261) [Error wrapped with additional info];", |
274
|
|
|
"<LAST_DIR>", params.PathHidingValue) |
275
|
|
|
|
276
|
|
|
// Check |
277
|
|
|
require.Equal(t, want, output) |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
func TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden(t *testing.T) { |
281
|
|
|
|
282
|
|
|
// Setup |
283
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
284
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
285
|
|
|
_, b, _, _ := runtime.Caller(0) |
286
|
|
|
path := strings.Split(filepath.Dir(b), string(os.PathSeparator)) |
287
|
|
|
params := e2hformat.Params{ |
288
|
|
|
PathHidingMethod: formatter.HidingMethod_ToFolder, |
289
|
|
|
PathHidingValue: path[len(path)-1], |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// Execute |
293
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
294
|
|
|
|
295
|
|
|
want := strings.ReplaceAll("{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden\",\"caller\":\"<LAST_DIR>/e2h_test.go:283\",\"context\":\"Error wrapped with additional info\"}]}", |
296
|
|
|
"<LAST_DIR>", params.PathHidingValue) |
297
|
|
|
|
298
|
|
|
// Check |
299
|
|
|
require.Equal(t, want, output) |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
func TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces(t *testing.T) { |
303
|
|
|
|
304
|
|
|
// Setup |
305
|
|
|
enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info") |
306
|
|
|
enhancedErr = e2h.Tracef(enhancedErr, "This is the %dnd. stack level", 2) |
307
|
|
|
enhancedErr = e2h.Trace(enhancedErr) |
308
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
309
|
|
|
_, b, _, _ := runtime.Caller(0) |
310
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
311
|
|
|
params := e2hformat.Params{ |
312
|
|
|
Beautify: true, |
313
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
314
|
|
|
PathHidingValue: hideThisPath, |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// Execute |
318
|
|
|
output := jsonFormatter.Format(enhancedErr, params) |
319
|
|
|
|
320
|
|
|
// Check |
321
|
|
|
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:305\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:306\",\n\t\t\t\"context\": \"This is the 2nd. stack level\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:307\"\n\t\t}\n\t]\n}") |
322
|
|
|
} |
323
|
|
|
|