forked from samber/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
117 lines (89 loc) · 3.25 KB
/
error_test.go
File metadata and controls
117 lines (89 loc) · 3.25 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
package oops
import (
"errors"
"io/fs"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrorsIs(t *testing.T) {
is := assert.New(t)
err := Errorf("Error: %w", fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Wrap(fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Wrapf(fs.ErrExist, "Error: %w", assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
err = Join(fs.ErrExist, assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
err = Join(assert.AnError, fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Recover(func() {
panic(fs.ErrExist)
})
is.True(errors.Is(err, fs.ErrExist))
err = Recoverf(func() {
panic(fs.ErrExist)
}, "Error: %w", assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
}
func TestErrorsAs(t *testing.T) {
is := assert.New(t)
var anError error = &fs.PathError{Err: fs.ErrExist}
var target *fs.PathError
err := Errorf("error: %w", anError)
is.True(errors.As(err, &target))
err = Wrap(anError)
is.True(errors.As(err, &target))
err = Wrapf(anError, "Error: %w", assert.AnError)
is.True(errors.As(err, &target))
err = Join(anError, assert.AnError)
is.True(errors.As(err, &target))
err = Join(assert.AnError, anError)
is.True(errors.As(err, &target))
err = Recover(func() {
panic(anError)
})
is.True(errors.As(err, &target))
err = Recoverf(func() {
panic(anError)
}, "Error: %w", assert.AnError)
is.True(errors.As(err, &target))
}
// TestErrorsIsWithOopsErrors tests the fix for comparing wrapped oops errors without panics
func TestErrorsIsWithOopsErrors(t *testing.T) {
is := assert.New(t)
// Test case 1: Compare wrapped oops error with original oops error
// This was the main use case that was causing panics
originalErr := New("user not found")
wrappedErr := In("User/GetByEmail").Wrap(originalErr)
// This should not panic and should return true
is.True(errors.Is(wrappedErr, originalErr))
// Test case 2: Compare multiple levels of wrapping
level1Err := New("database error")
level2Err := In("Repository").Wrap(level1Err)
level3Err := In("Service").Wrap(level2Err)
is.True(errors.Is(level3Err, level1Err))
is.True(errors.Is(level3Err, level2Err))
// Test case 3: Compare with different oops errors (should return false)
differentErr := New("different error")
is.False(errors.Is(wrappedErr, differentErr))
// Test case 4: Compare wrapped oops error with standard error
standardErr := errors.New("user not found")
wrappedStandardErr := In("User/GetByEmail").Wrap(standardErr)
is.True(errors.Is(wrappedStandardErr, standardErr))
// Test case 5: Compare oops error with wrapped standard error
oopsErr := New("user not found")
wrappedStandardErr2 := In("User/GetByEmail").Wrap(standardErr)
is.False(errors.Is(wrappedStandardErr2, oopsErr)) // Different error types
// Test case 6: Test with nil errors
is.False(errors.Is(wrappedErr, nil))
is.False(errors.Is(nil, originalErr))
// Test case 7: Test with same message but different instances
// Note: New() creates different underlying error instances, so they shouldn't match
err1 := New("same message")
err2 := New("same message")
is.False(errors.Is(err1, err2)) // Different instances, should not match
// Test case 8: Test with different messages
err3 := New("different message")
is.False(errors.Is(err1, err3))
}