-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
330 lines (273 loc) · 8.57 KB
/
errors_test.go
File metadata and controls
330 lines (273 loc) · 8.57 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package kit
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"testing"
)
type customError struct{ msg string }
func (e *customError) Error() string { return e.msg }
func newErr(t *testing.T, msg string) error {
t.Helper()
return errors.New(msg)
}
func TestAggregateError_Error_Empty(t *testing.T) {
var a AggregateError
if got := a.Error(); got != "" {
t.Fatalf("expected empty string, got %q", got)
}
}
func TestAggregateError_Error_WithErrors(t *testing.T) {
a := &AggregateError{
Errors: []error{
newErr(t, "first"),
newErr(t, "second"),
newErr(t, "third"),
},
}
got := a.Error()
parts := strings.Split(got, "; ")
if len(parts) != 3 {
t.Fatalf("expected 3 parts, got %d: %q", len(parts), got)
}
if parts[0] != "first" || parts[1] != "second" || parts[2] != "third" {
t.Fatalf("unexpected messages in Error(): %q", got)
}
}
func TestAggregateError_Unwrap_Empty(t *testing.T) {
var a AggregateError
if got := a.Unwrap(); got != nil {
t.Fatalf("expected nil, got %#v", got)
}
}
func TestAggregateError_Unwrap_NonEmpty(t *testing.T) {
errs := []error{newErr(t, "a"), newErr(t, "b")}
a := &AggregateError{Errors: errs}
got := a.Unwrap()
if got == nil {
t.Fatalf("expected non-nil slice")
}
if len(got) != len(errs) {
t.Fatalf("expected %d errors, got %d", len(errs), len(got))
}
// ensure it is the same underlying slice (as documented)
if &got[0] != &errs[0] {
t.Fatalf("expected underlying slice to be identical")
}
}
func TestAggregateError_Is(t *testing.T) {
target := newErr(t, "target")
wrapped := fmt.Errorf("wrapped: %w", target)
a := &AggregateError{
Errors: []error{
newErr(t, "other"),
wrapped,
},
}
if !a.Is(target) {
t.Fatalf("expected Is to find target error")
}
if a.Is(newErr(t, "nonexistent")) {
t.Fatalf("expected Is to return false for missing error")
}
}
func TestAggregateError_As(t *testing.T) {
ce := &customError{"x"}
a := &AggregateError{
Errors: []error{
newErr(t, "other"),
ce,
},
}
var target *customError
if !a.As(&target) {
t.Fatalf("expected As to find customErr")
}
if target != ce {
t.Fatalf("expected target to point to original customErr")
}
var notFound *MatrixError
if a.As(¬Found) {
t.Fatalf("expected As to return false for unmatched type")
}
}
func TestAggregateError_Join_NilReceiver(t *testing.T) {
var a *AggregateError
// Calling method on nil receiver should panic; verify behavior is defined.
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected panic on Join with nil receiver")
}
}()
_ = a.Join(newErr(t, "x"))
}
func TestAggregateError_Join_FilterNilAndReturnNilWhenEmpty(t *testing.T) {
a := &AggregateError{}
if got := a.Join(nil, nil); got != nil {
t.Fatalf("expected nil when only nil errors added, got %#v", got)
}
if a.Errors == nil {
t.Fatalf("expected internal slice to be initialized even when returning nil")
}
if len(a.Errors) != 0 {
t.Fatalf("expected internal slice to remain empty, got %d", len(a.Errors))
}
}
func TestAggregateError_Join_AddsErrorsAndReturnsSelf(t *testing.T) {
a := &AggregateError{}
e1 := newErr(t, "one")
e2 := newErr(t, "two")
got := a.Join(nil, e1, nil, e2)
if got != a {
t.Fatalf("expected Join to return receiver, got %#v", got)
}
if len(a.Errors) != 2 {
t.Fatalf("expected 2 errors, got %d", len(a.Errors))
}
if !errors.Is(a.Errors[0], e1) || !errors.Is(a.Errors[1], e2) {
t.Fatalf("unexpected errors slice: %#v", a.Errors)
}
}
func TestAggregateError_Join_Concurrent(t *testing.T) {
const goroutines = 10
const perGoroutine = 50
a := &AggregateError{}
var wg sync.WaitGroup
wg.Add(goroutines)
for g := 0; g < goroutines; g++ {
go func(id int) {
defer wg.Done()
for i := 0; i < perGoroutine; i++ {
a.Join(newErr(t, fmt.Sprintf("g%d-%d", id, i)))
}
}(g)
}
wg.Wait()
// We don't check exact messages (they're not important) but ensure no data race & count
if len(a.Errors) != goroutines*perGoroutine {
t.Fatalf("expected %d errors, got %d", goroutines*perGoroutine, len(a.Errors))
}
}
func TestNewAggregateError_NoErrorsReturnsNil(t *testing.T) {
if got := NewAggregateError(); got != nil {
t.Fatalf("expected nil, got %#v", got)
}
if got := NewAggregateError(nil, nil); got != nil {
t.Fatalf("expected nil, got %#v", got)
}
}
func TestNewAggregateError_WithErrors(t *testing.T) {
e1 := newErr(t, "one")
e2 := newErr(t, "two")
got := NewAggregateError(nil, e1, e2)
if got == nil {
t.Fatalf("expected non-nil aggregate")
}
if len(got.Errors) != 2 {
t.Fatalf("expected 2 errors, got %d", len(got.Errors))
}
if !errors.Is(got.Errors[0], e1) || !errors.Is(got.Errors[1], e2) {
t.Fatalf("unexpected errors slice: %#v", got.Errors)
}
}
// TestErrorResponseError tests the Error method of ErrorResponse
func TestErrorResponseError(t *testing.T) {
errResp := ErrorResponse{Err: "something went wrong"}
if got := errResp.Error(); got != "something went wrong" {
t.Errorf("ErrorResponse.Error() = %v, want %v", got, "something went wrong")
}
}
// TestNewErrorResponse tests the NewErrorResponse function
func TestNewErrorResponse(t *testing.T) {
err := errors.New("test error")
errResp := NewErrorResponse(err, http.StatusBadGateway)
if errResp == nil {
t.Fatal("NewErrorResponse() returned nil, expected non-nil")
}
if got := errResp.Err; got != "test error" {
t.Errorf("NewErrorResponse().Err = %v, want %v", got, "test error")
}
if got := errResp.StatusCode; got != http.StatusBadGateway {
t.Errorf("NewErrorResponse().StatusCode = %v, want %v", got, http.StatusBadGateway)
}
}
// TestNewErrorResponseNilError tests NewErrorResponse when the error is nil
func TestNewErrorResponseNilError(t *testing.T) {
errResp := NewErrorResponse(nil)
if errResp == nil {
t.Fatal("NewErrorResponse(nil) returned nil, expected non-nil")
}
if got := errResp.Err; got != "unknown error" {
t.Errorf("NewErrorResponse(nil).Err = %v, want %v", got, "unknown error")
}
}
// TestMatrixErrorError tests the Error method of MatrixError
func TestMatrixErrorError(t *testing.T) {
matrixErr := MatrixError{Err: "Matrix error"}
if got := matrixErr.Error(); got != "Matrix error" {
t.Errorf("MatrixError.Error() = %v, want %v", got, "Matrix error")
}
}
// TestNewMatrixError tests the NewMatrixError function
func TestNewMatrixError(t *testing.T) {
code := "M_UNKNOWN"
message := "Something went wrong"
matrixErr := NewMatrixError(code, message)
if matrixErr == nil {
t.Fatal("NewMatrixError() returned nil, expected non-nil")
}
if got := matrixErr.Code; got != code {
t.Errorf("NewMatrixError().Code = %v, want %v", got, code)
}
if got := matrixErr.Err; got != message {
t.Errorf("NewMatrixError().Err = %v, want %v", got, message)
}
}
// TestMatrixErrorFromSuccess tests MatrixErrorFrom with a valid JSON response
func TestMatrixErrorFromSuccess(t *testing.T) {
jsonStr := `{"errcode":"M_FORBIDDEN", "error":"Forbidden access"}`
reader := strings.NewReader(jsonStr)
matrixErr := MatrixErrorFrom(reader)
if matrixErr == nil {
t.Fatal("MatrixErrorFrom() returned nil, expected non-nil")
}
if got := matrixErr.Code; got != "M_FORBIDDEN" {
t.Errorf("MatrixErrorFrom().Code = %v, want %v", got, "M_FORBIDDEN")
}
if got := matrixErr.Err; got != "Forbidden access" {
t.Errorf("MatrixErrorFrom().Err = %v, want %v", got, "Forbidden access")
}
}
// TestMatrixErrorFromInvalidJSON tests MatrixErrorFrom with an invalid JSON response
func TestMatrixErrorFromInvalidJSON(t *testing.T) {
jsonStr := `{"errcode":"M_FORBIDDEN", "error":"Forbidden access"` // malformed JSON
reader := strings.NewReader(jsonStr)
matrixErr := MatrixErrorFrom(reader)
if matrixErr == nil {
t.Fatal("MatrixErrorFrom() returned nil, expected non-nil")
}
if got := matrixErr.Code; got != "M_UNKNOWN" {
t.Errorf("MatrixErrorFrom().Code = %v, want %v", got, "M_UNKNOWN")
}
if !strings.Contains(matrixErr.Err, "failed to decode error response") {
t.Errorf("MatrixErrorFrom().Err = %v, want it to contain 'failed to decode error response'", matrixErr.Err)
}
}
// TestMatrixErrorFromNilReader tests MatrixErrorFrom with a nil reader
func TestMatrixErrorFromNilReader(t *testing.T) {
matrixErr := MatrixErrorFrom(nil)
if matrixErr != nil {
t.Errorf("MatrixErrorFrom(nil) = %v, want nil", matrixErr)
}
}
// TestMatrixErrorFromEmptyReader tests MatrixErrorFrom with an empty reader
func TestMatrixErrorFromEmptyReader(t *testing.T) {
reader := strings.NewReader("")
expected := `failed to decode error response "": unexpected end of JSON input`
matrixErr := MatrixErrorFrom(reader)
if matrixErr.Error() != expected {
t.Errorf("MatrixErrorFrom(empty reader) = %v, want `%s`", matrixErr, expected)
}
}