This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomerror_test.go
297 lines (258 loc) · 7.49 KB
/
customerror_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
289
290
291
292
293
294
295
296
297
// Copyright 2021 The customerror Authors. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package customerror
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
const (
failedCreateSomethingMsg = "Failed to create something"
code = "E1010"
statusCode = http.StatusNotFound
)
var (
ErrFailedToReachServer = errors.New("failed to reach servers")
ErrFailedToReachServerDeep = fmt.Errorf("%s. %w", ErrFailedToReachServer, errors.New("servers are broken"))
ErrFailedToReachServerDeepRev = fmt.Errorf("%s. %w", errors.New("servers are broken"), ErrFailedToReachServer)
)
func TestNewLowLevel(t *testing.T) {
type args struct {
message string
opts []Option
}
tests := []struct {
name string
args args
want string
}{
{
name: "should work - with message",
args: args{message: failedCreateSomethingMsg},
want: failedCreateSomethingMsg,
},
{
name: "should work - with message, and code",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code)},
},
want: "E1010: Failed to create something",
},
{
name: "should work - with message, and error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithError(ErrFailedToReachServer)},
},
want: "Failed to create something. Original Error: Failed to reach servers",
},
{
name: "should work - with message, and deep error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithError(ErrFailedToReachServerDeep)},
},
want: "Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
{
name: "should work - with message, and status code",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithStatusCode(statusCode)},
},
want: "Failed to create something",
},
{
name: "should work - with message, code, and error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServer)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers",
},
{
name: "should work - with message, code, error, and deep error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServerDeep)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
{
name: "should work - with message, code, error, deep error, and status code",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServerDeep), WithStatusCode(statusCode)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := New(tt.args.message, tt.args.opts...)
if !strings.EqualFold(got.Error(), tt.want) {
t.Errorf("NewLowLevel() = %v, want %v", got, tt.want)
}
})
}
}
func TestBuiltin(t *testing.T) {
ErrFailedToCreateFile := NewFailedToError("create file")
ErrInvalidPath := NewInvalidError("path")
ErrMissingPath := NewMissingError("path")
ErrRequiredPath := NewRequiredError("path is")
testFunc := func(e error) error { return e }
type args struct {
err error
}
tests := []struct {
name string
args args
want string
wantAs string
}{
{
name: "Should work - ErrFailedToCreateFile",
args: args{
err: ErrFailedToCreateFile,
},
want: "failed to create file",
wantAs: "failed to create file",
},
{
name: "Should work - ErrInvalidPath",
args: args{
err: ErrInvalidPath,
},
want: "invalid path",
wantAs: "invalid path",
},
{
name: "Should work - ErrMissingPath",
args: args{
err: ErrMissingPath,
},
want: "missing path",
wantAs: "missing path",
},
{
name: "Should work - ErrRequiredPath",
args: args{
err: ErrRequiredPath,
},
want: "path is required",
wantAs: "path is required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := testFunc(tt.args.err)
if !errors.Is(err, tt.args.err) {
t.Errorf("Expected error to be (is) %v, got %v", tt.args.err, err)
}
errWrapped := fmt.Errorf("Wrapped %w", err)
if !errors.Is(errWrapped, tt.args.err) {
t.Errorf("Expected error to be (is - wrapped) %v, got %v", tt.args.err, errWrapped)
}
if !strings.EqualFold(err.Error(), tt.want) {
t.Errorf(`Expected message to be "%v", got "%v"`, tt.want, err)
}
var errAs *CustomError
if errors.As(err, &errAs) {
if errAs.Message != tt.wantAs {
t.Errorf(`Expected message to be (As)"%v", got "%v"`, tt.wantAs, errAs.Message)
}
}
})
}
}
func TestCustomError_Unwrap(t *testing.T) {
type fields struct {
Code string
Err error
Message string
StatusCode int
}
tests := []struct {
name string
fields fields
wantErr bool
want string
}{
{
name: "Should work",
fields: fields{
Code: "",
Err: errors.New("Wrapped error"),
Message: "Main error",
StatusCode: 0,
},
wantErr: true,
want: "Wrapped error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cE := &CustomError{
Code: tt.fields.Code,
Err: tt.fields.Err,
Message: tt.fields.Message,
StatusCode: tt.fields.StatusCode,
}
err := cE.Unwrap()
if (err != nil) != tt.wantErr {
t.Errorf("CustomError.Unwrap() error = %v, wantErr %v", err, tt.wantErr)
}
if err.Error() != tt.want {
t.Errorf("CustomError.Unwrap() message = %v, want %v", err, tt.want)
}
})
}
}
func TestNew_deepNestedErrors(t *testing.T) {
expectedErrMsg := "custom message. Original Error: layer 3. layer 2. layer 1"
layer1 := errors.New("layer 1")
layer2 := fmt.Errorf("layer 2. %w", layer1)
layer3 := fmt.Errorf("layer 3. %w", layer2)
ErrLayered := New("custom message", WithError(layer3))
if ErrLayered.Error() != expectedErrMsg {
t.Errorf("CustomError deep nested errors got %s, want %s", ErrLayered, expectedErrMsg)
}
testFunc := func() error { return ErrLayered }
errLayered := testFunc()
if !errors.Is(errLayered, ErrLayered) {
t.Errorf("Expected %v be ErrLayered", errLayered)
}
errSome := errors.New("Some error")
errWrapped := Wrap(errLayered, errSome)
if !errors.Is(errWrapped, ErrLayered) {
t.Errorf("Expected %v be ErrLayered", errWrapped)
}
expectedErrWrappedMsg := "custom message. Original Error: layer 3. layer 2. layer 1. Wrapped Error(s): Some error"
if errWrapped.Error() != expectedErrWrappedMsg {
t.Errorf("Expected %v to be %s", errWrapped.Error(), expectedErrWrappedMsg)
}
}
func TestWrap(t *testing.T) {
expectedErrMsg := "custom message. Original Error: layer 3. layer 2. layer 1"
layer1 := errors.New("layer 1")
layer2 := fmt.Errorf("layer 2. %w", layer1)
layer3 := fmt.Errorf("layer 3. %w", layer2)
ErrLayered := New("custom message", WithError(layer3))
if ErrLayered.Error() != expectedErrMsg {
t.Errorf("Wrap got %s, want %s", ErrLayered, expectedErrMsg)
}
testFunc := func() error { return ErrLayered }
errLayered := testFunc()
if !errors.Is(errLayered, ErrLayered) {
t.Errorf("Wrap Is got %s, want %s", errLayered, ErrLayered)
}
errSome := errors.New("Some error")
if !errors.Is(Wrap(errLayered, errSome), ErrLayered) {
t.Errorf("Wrap Is got %s, want %s", errSome, ErrLayered)
}
}