forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
287 lines (278 loc) · 7.01 KB
/
errors_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
package influxdb_test
import (
"encoding/json"
"errors"
"fmt"
"testing"
platform "github.com/influxdata/influxdb"
)
const EFailedToGetStorageHost = "failed to get the storage host"
func TestErrorMsg(t *testing.T) {
cases := []struct {
name string
err error
msg string
}{
{
name: "simple error",
err: &platform.Error{Code: platform.ENotFound},
msg: "<not found>",
},
{
name: "with op",
err: &platform.Error{
Code: platform.ENotFound,
Op: "bolt.FindAuthorizationByID",
},
msg: "bolt.FindAuthorizationByID: <not found>",
},
{
name: "with op and value",
err: &platform.Error{
Code: platform.ENotFound,
Op: "bolt.FindAuthorizationByID",
Msg: fmt.Sprintf("with ID %d", 323),
},
msg: "bolt.FindAuthorizationByID: <not found> with ID 323",
},
{
name: "with a third party error",
err: &platform.Error{
Code: EFailedToGetStorageHost,
Op: "cmd/fluxd.injectDeps",
Err: errors.New("empty value"),
},
msg: "cmd/fluxd.injectDeps: empty value",
},
{
name: "with a internal error",
err: &platform.Error{
Code: EFailedToGetStorageHost,
Op: "cmd/fluxd.injectDeps",
Err: &platform.Error{Code: platform.EEmptyValue, Op: "cmd/fluxd.getStrList"},
},
msg: "cmd/fluxd.injectDeps: cmd/fluxd.getStrList: <empty value>",
},
}
for _, c := range cases {
if c.msg != c.err.Error() {
t.Errorf("%s failed, want %s, got %s", c.name, c.msg, c.err.Error())
}
}
}
func TestErrorMessage(t *testing.T) {
cases := []struct {
name string
err error
want string
}{
{
name: "nil error",
},
{
name: "nil error of type *platform.Error",
err: (*platform.Error)(nil),
},
{
name: "simple error",
err: &platform.Error{Msg: "simple error"},
want: "simple error",
},
{
name: "embedded error",
err: &platform.Error{Err: &platform.Error{Msg: "embedded error"}},
want: "embedded error",
},
{
name: "default error",
err: errors.New("s"),
want: "An internal error has occurred.",
},
}
for _, c := range cases {
if result := platform.ErrorMessage(c.err); c.want != result {
t.Errorf("%s failed, want %s, got %s", c.name, c.want, result)
}
}
}
func TestErrorOp(t *testing.T) {
cases := []struct {
name string
err error
want string
}{
{
name: "nil error",
},
{
name: "nil error of type *platform.Error",
err: (*platform.Error)(nil),
},
{
name: "simple error",
err: &platform.Error{Op: "op1"},
want: "op1",
},
{
name: "embedded error",
err: &platform.Error{Op: "op1", Err: &platform.Error{Code: platform.EInvalid}},
want: "op1",
},
{
name: "embedded error without op in root level",
err: &platform.Error{Err: &platform.Error{Code: platform.EInvalid, Op: "op2"}},
want: "op2",
},
{
name: "default error",
err: errors.New("s"),
want: "",
},
}
for _, c := range cases {
if result := platform.ErrorOp(c.err); c.want != result {
t.Errorf("%s failed, want %s, got %s", c.name, c.want, result)
}
}
}
func TestErrorCode(t *testing.T) {
cases := []struct {
name string
err error
want string
}{
{
name: "nil error",
},
{
name: "nil error of type *platform.Error",
err: (*platform.Error)(nil),
},
{
name: "simple error",
err: &platform.Error{Code: platform.ENotFound},
want: platform.ENotFound,
},
{
name: "embedded error",
err: &platform.Error{Code: platform.ENotFound, Err: &platform.Error{Code: platform.EInvalid}},
want: platform.ENotFound,
},
{
name: "embedded error with root level code",
err: &platform.Error{Err: &platform.Error{Code: platform.EInvalid}},
want: platform.EInvalid,
},
{
name: "default error",
err: errors.New("s"),
want: platform.EInternal,
},
}
for _, c := range cases {
if result := platform.ErrorCode(c.err); c.want != result {
t.Errorf("%s failed, want %s, got %s", c.name, c.want, result)
}
}
}
func TestJSON(t *testing.T) {
cases := []struct {
name string
err *platform.Error
encoded string
}{
{
name: "simple error",
err: &platform.Error{Code: platform.ENotFound},
encoded: `{"code":"not found"}`,
},
{
name: "with op",
err: &platform.Error{
Code: platform.ENotFound,
Op: "bolt.FindAuthorizationByID",
},
encoded: `{"code":"not found","op":"bolt.FindAuthorizationByID"}`,
},
{
name: "with op and value",
err: &platform.Error{
Code: platform.ENotFound,
Op: "bolt/FindAuthorizationByID",
Msg: fmt.Sprintf("with ID %d", 323),
},
encoded: `{"code":"not found","message":"with ID 323","op":"bolt/FindAuthorizationByID"}`,
},
{
name: "with a third party error",
err: &platform.Error{
Code: EFailedToGetStorageHost,
Op: "cmd/fluxd.injectDeps",
Err: errors.New("empty value"),
},
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":"empty value"}`,
},
{
name: "with a internal error",
err: &platform.Error{
Code: EFailedToGetStorageHost,
Op: "cmd/fluxd.injectDeps",
Err: &platform.Error{Code: platform.EEmptyValue, Op: "cmd/fluxd.getStrList"},
},
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":{"code":"empty value","op":"cmd/fluxd.getStrList"}}`,
},
{
name: "with a deep internal error",
err: &platform.Error{
Code: EFailedToGetStorageHost,
Op: "cmd/fluxd.injectDeps",
Err: &platform.Error{
Code: platform.EInvalid,
Op: "cmd/fluxd.getStrList",
Err: &platform.Error{
Code: platform.EEmptyValue,
Err: errors.New("an err"),
},
},
},
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":{"code":"invalid","op":"cmd/fluxd.getStrList","error":{"code":"empty value","error":"an err"}}}`,
},
}
for _, c := range cases {
result, err := json.Marshal(c.err)
// encode testing
if err != nil {
t.Errorf("%s encode failed, want err: %v, should be nil", c.name, err)
}
if string(result) != c.encoded {
t.Errorf("%s encode failed, want result: %s, got %s", c.name, c.encoded, string(result))
}
// decode testing
got := new(platform.Error)
err = json.Unmarshal(result, got)
if err != nil {
t.Errorf("%s decode failed, want err: %v, should be nil", c.name, err)
}
decodeEqual(t, c.err, got, "decode: "+c.name)
}
}
func decodeEqual(t *testing.T, want, result *platform.Error, caseName string) {
if want.Code != result.Code {
t.Errorf("%s code failed, want %s, got %s", caseName, want.Code, result.Code)
}
if want.Op != result.Op {
t.Errorf("%s op failed, want %s, got %s", caseName, want.Op, result.Op)
}
if want.Msg != result.Msg {
t.Errorf("%s msg failed, want %s, got %s", caseName, want.Msg, result.Msg)
}
if want.Err != nil {
if _, ok := want.Err.(*platform.Error); ok {
decodeEqual(t, want.Err.(*platform.Error), result.Err.(*platform.Error), caseName)
} else {
if want.Err.Error() != result.Err.Error() {
t.Errorf("%s Err failed, want %s, got %s", caseName, want.Err.Error(), result.Err.Error())
}
}
}
}