-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
139 lines (125 loc) · 2.77 KB
/
error_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
package k6build
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
)
func Test_WrappedError(t *testing.T) {
t.Parallel()
var (
err = errors.New("error")
reason = errors.New("reason")
)
testCases := []struct {
title string
err error
reason error
expect []error
}{
{
title: "error and reason",
err: err,
reason: reason,
expect: []error{err, reason},
},
{
title: "error not reason",
err: err,
reason: nil,
expect: []error{err},
},
{
title: "multiple and reasons",
err: err,
reason: reason,
expect: []error{err, reason},
},
{
title: "wrapped err",
err: fmt.Errorf("wrapped %w", err),
reason: reason,
expect: []error{err, reason},
},
{
title: "wrapped reason",
err: err,
reason: fmt.Errorf("wrapped %w", reason),
expect: []error{err, reason},
},
{
title: "wrapped err in target",
err: err,
reason: reason,
expect: []error{fmt.Errorf("wrapped %w", err)},
},
{
title: "wrapped reason in target",
err: err,
reason: reason,
expect: []error{fmt.Errorf("wrapped %w", reason)},
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
err := NewWrappedError(tc.err, tc.reason)
for _, expected := range tc.expect {
if !errors.Is(err, expected) {
t.Fatalf("expected %v got %v", expected, err)
}
}
})
}
}
func Test_JsonSerialization(t *testing.T) {
t.Parallel()
var (
err = errors.New("error")
reason = errors.New("reason")
root = errors.New("root")
)
testCases := []struct {
title string
err *WrappedError
expect []byte
}{
{
title: "error with cause",
err: NewWrappedError(err, reason),
expect: []byte(`{"error":"error","reason":{"error":"reason"}}`),
},
{
title: "error with nested causes",
err: NewWrappedError(err, NewWrappedError(reason, root)),
expect: []byte(`{"error":"error","reason":{"error":"reason","reason":{"error":"root"}}}`),
},
{
title: "error with nil cause",
err: NewWrappedError(err, nil),
expect: []byte(`{"error":"error","reason":{"error":"reason unknown"}}`),
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
marshalled, err := json.Marshal(tc.err)
if err != nil {
t.Fatalf("error marshaling: %v", err)
}
if !bytes.Equal(marshalled, tc.expect) {
t.Fatalf("failed unmarshaling expected %v got %v", string(tc.expect), string(marshalled))
}
unmashalled := &WrappedError{}
err = json.Unmarshal(marshalled, unmashalled)
if err != nil {
t.Fatalf("error unmashaling: %v", err)
}
if !reflect.DeepEqual(tc.err, unmashalled) {
t.Fatalf("failed marshaling expected %v got %v", tc.err, unmashalled)
}
})
}
}