-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_node_test.go
288 lines (269 loc) · 6.08 KB
/
error_node_test.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
package tower
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/kinbiko/jsonassert"
"strings"
"testing"
"time"
)
func TestErrorNode_CodeBlockJSON(t *testing.T) {
tests := []struct {
name string
baseError error
messages []string
want string
wantErr bool
}{
{
name: "expected output",
baseError: errors.New("base error"),
messages: []string{"message 1", "message 2", "message 3"},
want: `
{
"message": "message 3",
"caller": "<<PRESENCE>>",
"error": {
"message": "message 2",
"caller": "<<PRESENCE>>",
"error": {
"message": "message 1",
"caller": "<<PRESENCE>>",
"error": {
"time": "<<PRESENCE>>",
"code": 500,
"message": "base error",
"caller": "<<PRESENCE>>",
"level": "error",
"service": {
"name": "testing-code-block",
"environment": "testing",
"type": "unit-test"
},
"error": {
"summary": "base error"
}
}
}
}
}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tow := NewTower(Service{
Name: "testing-code-block",
Environment: "testing",
Type: "unit-test",
})
err := tow.Wrap(tt.baseError).Freeze()
for _, e := range tt.messages {
err = tow.WrapFreeze(err, e)
}
got, errCB := err.(*ErrorNode).CodeBlockJSON()
if (errCB != nil) != tt.wantErr {
t.Errorf("ErrorNode.CodeBlockJSON() error = %v, wantErr %v", errCB, tt.wantErr)
return
}
j := jsonassert.New(t)
j.Assertf(string(got), tt.want)
if t.Failed() {
fmt.Println(string(got))
}
if !strings.Contains(string(got), "error_node_test.go") {
t.Error("expected to see caller in error_node_test.go")
}
if strings.Count(string(got), "error_node_test.go") != 4 {
t.Error("expected to see four callers field in error_node_test.go")
}
})
}
}
type mockImplError struct{}
func (m mockImplError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"message": m.Message(),
"caller": m.Caller(),
"code": m.Code(),
"error": m.Error(),
"level": m.Level(),
"service": m.Service(),
"time": m.Time(),
})
}
func (m mockImplError) Error() string {
return "mock"
}
func (m mockImplError) Caller() Caller {
return GetCaller(1)
}
func (m mockImplError) Code() int {
return 500
}
func (m mockImplError) Context() []any {
return nil
}
func (m mockImplError) Unwrap() error {
return nil
}
func (m mockImplError) WriteError(w LineWriter) {}
func (m mockImplError) HTTPCode() int {
return 500
}
func (m mockImplError) Key() string {
return ""
}
func (m mockImplError) Level() Level {
return ErrorLevel
}
func (m mockImplError) Message() string {
return "mocking time"
}
func (m mockImplError) Time() time.Time {
return time.Now()
}
func (m mockImplError) Service() Service {
return Service{
Name: "mock",
}
}
func (m mockImplError) Log(ctx context.Context) Error {
return m
}
func (m mockImplError) Notify(ctx context.Context, opts ...MessageOption) Error {
return m
}
func TestErrorNode_MarshalJSON(t *testing.T) {
tow := NewTower(Service{Name: "test"})
tests := []struct {
name string
err *ErrorNode
want string
wantErr bool
}{
{
name: "expected output - simple error",
err: tow.Wrap(errors.New("base error")).Message("bar").Freeze().(*ErrorNode),
want: `
{
"time": "<<PRESENCE>>",
"code": 500,
"message": "bar",
"caller": "<<PRESENCE>>",
"level": "error",
"service": {
"name": "test"
},
"error": {
"summary": "base error"
}
}`,
wantErr: false,
},
{
name: "expected output - nested error",
err: func() *ErrorNode {
base := tow.Wrap(errors.New("base error")).Message("bar").Freeze()
return tow.WrapFreeze(base, "error 1").(*ErrorNode)
}(),
want: `
{
"time": "<<PRESENCE>>",
"code": 500,
"message": "error 1",
"caller": "<<PRESENCE>>",
"level": "error",
"service": {
"name": "test"
},
"error": {
"message": "bar",
"caller": "<<PRESENCE>>",
"error": {
"summary": "base error"
}
}
}`,
wantErr: false,
},
{
name: "expected output - nested error with wrap that does nothing",
err: func() *ErrorNode {
base := tow.Wrap(errors.New("base error")).Message("bar").Freeze()
base = tow.Wrap(base).Freeze()
base = tow.Wrap(base).Freeze()
return tow.WrapFreeze(base, "error 1").(*ErrorNode)
}(),
want: `
{
"time": "<<PRESENCE>>",
"code": 500,
"message": "error 1",
"caller": "<<PRESENCE>>",
"level": "error",
"service": {
"name": "test"
},
"error": {
"message": "bar",
"caller": "<<PRESENCE>>",
"error": {
"summary": "base error"
}
}
}`,
wantErr: false,
},
{
name: "expected output - wrap other Error implementation",
err: func() *ErrorNode {
base := tow.Wrap(mockImplError{}).Code(400).Freeze()
return tow.WrapFreeze(base, "error 1").(*ErrorNode)
}(),
want: `
{
"time": "<<PRESENCE>>",
"code": 400,
"message": "error 1",
"caller": "<<PRESENCE>>",
"level": "error",
"service": {
"name": "test"
},
"error": {
"caller": "<<PRESENCE>>",
"code": 500,
"error": "mock",
"level": 2,
"message": "mocking time",
"service": {
"name": "mock"
},
"time": "<<PRESENCE>>"
}
}`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := tt.err
got, err := e.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
j := jsonassert.New(t)
j.Assertf(string(got), tt.want)
if t.Failed() {
out := new(bytes.Buffer)
_ = json.Indent(out, got, "", " ")
fmt.Println(out.String())
}
})
}
}