-
Notifications
You must be signed in to change notification settings - Fork 1
/
xerror_test.go
187 lines (160 loc) · 5.12 KB
/
xerror_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
package xerror_test
import (
"errors"
"testing"
"time"
"github.com/seefs001/xox/xerror"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
err := xerror.New("test error")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "test error")
assert.NotEmpty(t, err.Stack)
}
func TestNewWithCode(t *testing.T) {
err := xerror.NewWithCode("test error", 500)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "test error")
assert.Equal(t, 500, err.Code)
}
func TestWrap(t *testing.T) {
originalErr := errors.New("original error")
wrappedErr := xerror.Wrap(originalErr, "wrapped error")
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "wrapped error: original error")
}
func TestWrapWithCode(t *testing.T) {
originalErr := errors.New("original error")
wrappedErr := xerror.WrapWithCode(originalErr, "wrapped error", 400)
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "wrapped error: original error")
assert.Equal(t, 400, wrappedErr.Code)
}
func TestIs(t *testing.T) {
err := xerror.New("test error")
assert.True(t, xerror.Is(err, err))
assert.False(t, xerror.Is(err, errors.New("different error")))
}
func TestAs(t *testing.T) {
err := xerror.New("test error")
var xerr *xerror.Error
assert.True(t, xerror.As(err, &xerr))
assert.NotNil(t, xerr)
}
func TestWithContext(t *testing.T) {
err := xerror.New("test error")
contextErr := xerror.WithContext(err, "key", "value")
assert.Equal(t, "value", contextErr.Context["key"])
}
func TestIsTemporary(t *testing.T) {
tempErr := &temporaryError{}
assert.True(t, xerror.IsTemporary(tempErr))
assert.False(t, xerror.IsTemporary(errors.New("non-temporary error")))
}
func TestRetry(t *testing.T) {
count := 0
err := xerror.Retry(3, time.Millisecond, func() error {
count++
if count < 3 {
return &temporaryError{}
}
return nil
})
assert.Nil(t, err)
assert.Equal(t, 3, count)
}
func TestGetErrorCode(t *testing.T) {
assert.Equal(t, xerror.CodeNotFound, xerror.GetErrorCode(xerror.ErrNotFound))
assert.Equal(t, xerror.CodeUnauthorized, xerror.GetErrorCode(xerror.ErrUnauthorized))
assert.Equal(t, xerror.CodeInvalidInput, xerror.GetErrorCode(xerror.ErrInvalidInput))
assert.Equal(t, xerror.CodeUnknown, xerror.GetErrorCode(errors.New("unknown error")))
}
func TestFormatError(t *testing.T) {
err := xerror.NewWithCode("test error", 500)
formatted := xerror.FormatError(err)
assert.Contains(t, formatted, "test error")
assert.Contains(t, formatted, "Code: 500")
assert.Contains(t, formatted, "Stack Trace:")
}
func TestNewErrorf(t *testing.T) {
err := xerror.NewErrorf("error %d: %s", 1, "test")
assert.Contains(t, err.Error(), "error 1: test")
}
func TestWrapWithContextf(t *testing.T) {
originalErr := errors.New("original error")
err := xerror.WrapWithContextf(originalErr, "key", "value", "wrapped error %d", 1)
assert.Contains(t, err.Error(), "wrapped error 1: original error")
assert.Equal(t, "value", err.Context["key"])
}
func TestIsErrorCode(t *testing.T) {
err := xerror.NewWithCode("test error", int(xerror.CodeNotFound))
assert.True(t, xerror.IsErrorCode(err, xerror.CodeNotFound))
assert.False(t, xerror.IsErrorCode(err, xerror.CodeUnauthorized))
}
// Helper struct for testing IsTemporary
type temporaryError struct{}
func (e *temporaryError) Error() string { return "temporary error" }
func (e *temporaryError) Temporary() bool { return true }
func TestWrapf(t *testing.T) {
originalErr := errors.New("original error")
wrappedErr := xerror.Wrapf(originalErr, "wrapped error %d", 1)
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Err.Error(), "wrapped error 1: original error")
}
func TestNewf(t *testing.T) {
err := xerror.Newf("error %d: %s", 1, "test")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "error 1: test")
}
func TestErrorf(t *testing.T) {
err := xerror.Errorf("error %d: %s", 1, "test")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "error 1: test")
}
func TestPanicIfError(t *testing.T) {
assert.NotPanics(t, func() {
xerror.PanicIfError(nil)
})
assert.Panics(t, func() {
xerror.PanicIfError(errors.New("test error"))
})
}
func TestRecoverError(t *testing.T) {
var err error
func() {
defer xerror.RecoverError(&err)
panic("test panic")
}()
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "test panic")
}
func TestMustNoError(t *testing.T) {
result := xerror.MustNoError(42, nil)
assert.Equal(t, 42, result)
assert.Panics(t, func() {
xerror.MustNoError(0, errors.New("test error"))
})
}
func TestCause(t *testing.T) {
rootErr := errors.New("root error")
wrappedErr := xerror.Wrap(rootErr, "wrapped error")
cause := xerror.Cause(wrappedErr)
assert.Equal(t, rootErr, cause)
}
func TestJoin(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
joinedErr := xerror.Join(err1, err2)
assert.NotNil(t, joinedErr)
assert.Contains(t, joinedErr.Error(), "error 1")
assert.Contains(t, joinedErr.Error(), "error 2")
}
func TestWithStack(t *testing.T) {
err := errors.New("test error")
stackErr := xerror.WithStack(err)
assert.NotNil(t, stackErr)
xerr, ok := stackErr.(*xerror.Error)
assert.True(t, ok)
assert.NotEmpty(t, xerr.Stack)
}