-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors_test.go
283 lines (216 loc) · 7.22 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
package ginerr
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// Register functions are tested through NewErrorResponse[E error]
type AError struct {
message string
}
func (e *AError) Error() string {
return e.message
}
type BError struct {
message string
}
func (e *BError) Error() string {
return e.message
}
type dummyContextKey string
// The top ones are not parallel because it uses the DefaultErrorRegistry, which is a global
//nolint:paralleltest // Can't be used, we use global variables
func TestErrorResponse_UsesDefaultErrorRegistry(t *testing.T) {
// Arrange
expectedResponse := "user-friendly error"
var calledWithError *AError
callback := func(_ context.Context, err *AError) (int, any) {
calledWithError = err
return 634, expectedResponse
}
err := &AError{message: "It was the man with one hand!"}
RegisterErrorHandler(&AError{}, callback)
// Act
code, response := NewErrorResponse(context.Background(), err)
// Assert
assert.Equal(t, err, calledWithError)
assert.Equal(t, 634, code)
assert.Equal(t, expectedResponse, response)
}
// These are parallel because it uses the 'from' variant
func TestErrorResponseFrom_ReturnsNullOnNoDefaultErrorDefined(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
// Act
code, response := NewErrorResponseFrom(context.Background(), registry, assert.AnError)
// Assert
assert.Equal(t, http.StatusInternalServerError, code)
assert.Nil(t, response)
}
func TestErrorResponseFrom_UsesDefaultCallbackOnNotFound(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
var calledWithErr error
var calledWithCtx context.Context
callback := func(ctx context.Context, err error) (int, any) {
calledWithErr = err
calledWithCtx = ctx
return http.StatusPaymentRequired, "abc"
}
registry.RegisterDefaultHandler(callback)
// Dummy value to check whether it was passed correctly
ctx := context.WithValue(context.Background(), dummyContextKey("abc"), "good")
// Act
code, response := NewErrorResponseFrom(ctx, registry, assert.AnError)
// Assert
assert.Equal(t, "abc", response)
assert.Equal(t, http.StatusPaymentRequired, code)
assert.Equal(t, ctx, calledWithCtx)
assert.Equal(t, assert.AnError, calledWithErr)
}
func TestErrorResponseFrom_ReturnsExpectedResponsesOnErrorTypes(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
var calledWithErrA *AError
var calledWithCtxA context.Context
callbackA := func(ctx context.Context, err *AError) (int, any) {
calledWithErrA = err
calledWithCtxA = ctx
return 123, "abc"
}
var calledWithErrB *BError
var calledWithCtxB context.Context
callbackB := func(ctx context.Context, err *BError) (int, any) {
calledWithErrB = err
calledWithCtxB = ctx
return 456, "def"
}
errA := &AError{message: "It was the man with one hand!"}
errB := &BError{message: "It was the woman with 3 hands!"}
RegisterErrorHandlerOn(registry, &AError{}, callbackA)
RegisterErrorHandlerOn(registry, &BError{}, callbackB)
ctx := context.WithValue(context.Background(), dummyContextKey("abc"), "cool")
// Act
codeA, responseA := NewErrorResponseFrom(ctx, registry, errA)
codeB, responseB := NewErrorResponseFrom(ctx, registry, errB)
// Assert
assert.Equal(t, 123, codeA)
assert.Equal(t, "abc", responseA)
assert.Equal(t, errA, calledWithErrA)
assert.Equal(t, ctx, calledWithCtxA)
assert.Equal(t, 456, codeB)
assert.Equal(t, "def", responseB)
assert.Equal(t, errB, calledWithErrB)
assert.Equal(t, ctx, calledWithCtxB)
}
func TestErrorResponseFrom_ReturnsExpectedResponsesOnErrorStrings(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
var calledWithErrA error
var calledWithCtxA context.Context
callbackA := func(ctx context.Context, err error) (int, any) {
calledWithErrA = err
calledWithCtxA = ctx
return 123, "abc"
}
var calledWithErrB error
var calledWithCtxB context.Context
callbackB := func(ctx context.Context, err error) (int, any) {
calledWithErrB = err
calledWithCtxB = ctx
return 456, "def"
}
errA := errors.New("error A")
errB := errors.New("error B")
RegisterErrorHandlerOn(registry, errA, callbackA)
RegisterErrorHandlerOn(registry, errB, callbackB)
ctx := context.WithValue(context.Background(), dummyContextKey("abc"), "cool")
// Act
codeA, responseA := NewErrorResponseFrom(ctx, registry, fmt.Errorf("error A: %w", errA))
codeB, responseB := NewErrorResponseFrom(ctx, registry, fmt.Errorf("error B: %w", errB))
// Assert
assert.Equal(t, 123, codeA)
assert.Equal(t, "abc", responseA)
assert.Equal(t, errA, calledWithErrA)
assert.Equal(t, ctx, calledWithCtxA)
assert.Equal(t, 456, codeB)
assert.Equal(t, "def", responseB)
assert.Equal(t, errB, calledWithErrB)
assert.Equal(t, ctx, calledWithCtxB)
}
func TestErrorResponseFrom_HandlesErrorsFromMethodsProperly(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
expectedResponse := "user-friendly error"
var calledWithErr *AError
callback := func(_ context.Context, err *AError) (int, any) {
calledWithErr = err
return http.StatusPaymentRequired, expectedResponse
}
err := func() error {
return &AError{message: "It was the man with one hand!"}
}()
RegisterErrorHandlerOn(registry, &AError{}, callback)
// Act
code, response := NewErrorResponseFrom(context.Background(), registry, err)
// Assert
assert.Equal(t, http.StatusPaymentRequired, code)
assert.Equal(t, expectedResponse, response)
assert.Equal(t, &AError{message: "It was the man with one hand!"}, calledWithErr)
}
func TestErrorResponseFrom_HandlesWrappedErrorsProperly(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
expectedResponse := "user-friendly error"
var calledWithErr *AError
callback := func(_ context.Context, err *AError) (int, any) {
calledWithErr = err
return http.StatusInternalServerError, expectedResponse
}
err := fmt.Errorf("this error happened: %w", &AError{message: "abc"})
RegisterErrorHandlerOn(registry, &AError{}, callback)
// Act
code, response := NewErrorResponseFrom(context.Background(), registry, err)
// Assert
assert.Equal(t, http.StatusInternalServerError, code)
assert.Equal(t, expectedResponse, response)
assert.Equal(t, &AError{message: "abc"}, calledWithErr)
}
func TestErrorResponseFrom_ReturnsErrorBInInterface(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
expectedResponse := "user-friendly error"
var calledWithErr error
callback := func(_ context.Context, err *BError) (int, any) {
calledWithErr = err
return http.StatusInternalServerError, expectedResponse
}
var err error = &BError{message: "It was the man with one hand!"}
RegisterErrorHandlerOn(registry, &BError{}, callback)
// Act
code, response := NewErrorResponseFrom(context.Background(), registry, err)
// Assert
assert.Equal(t, calledWithErr, err)
assert.Equal(t, http.StatusInternalServerError, code)
assert.Equal(t, expectedResponse, response)
}
func TestErrorResponseFrom_ReturnsGenericErrorOnTypeNotFound(t *testing.T) {
t.Parallel()
// Arrange
registry := NewErrorRegistry()
// Act
code, response := NewErrorResponseFrom(context.Background(), registry, &BError{})
// Assert
assert.Equal(t, http.StatusInternalServerError, code)
assert.Nil(t, response)
}