-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_test.go
More file actions
350 lines (300 loc) · 10.1 KB
/
Copy pathfunc_test.go
File metadata and controls
350 lines (300 loc) · 10.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package xerror_test
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/gomooth/xerror"
"github.com/gomooth/xerror/xcode"
)
func TestNew(t *testing.T) {
err := xerror.New("this is new error")
if err.Message() != "this is new error" {
t.Fatalf("expected 'this is new error', got %q", err.Message())
}
if err.ErrorCode() != xcode.CodeNone {
t.Fatalf("New() should default to CodeNone(0), got %d", err.ErrorCode())
}
}
func TestNewCode(t *testing.T) {
err := xerror.NewCode(100001, "未登录")
if err.ErrorCode() != 100001 {
t.Fatalf("expected code 100001, got %d", err.ErrorCode())
}
if err.Message() != "未登录" {
t.Fatalf("expected '未登录', got %q", err.Message())
}
msg := err.ToMessage(nil)
if msg != "未登录" {
t.Fatalf("expected '未登录', got %q", msg)
}
}
func TestNewCodef(t *testing.T) {
err := xerror.NewCodef(100002, "用户 %s 不存在", "test")
if err.Message() != "用户 test 不存在" {
t.Fatalf("expected '用户 test 不存在', got %q", err.Message())
}
}
func TestNewXCode(t *testing.T) {
err := xerror.NewXCode(xcode.InternalServerError)
if err.ErrorCode() != xcode.InternalServerError.Code() {
t.Fatalf("expected %d, got %d", xcode.InternalServerError.Code(), err.ErrorCode())
}
}
func TestNewXCode_withMessage(t *testing.T) {
err := xerror.NewXCode(xcode.InternalServerError, "变更消息")
if err.Message() != "变更消息" {
t.Fatalf("expected '变更消息', got %q", err.Message())
}
}
func TestParsePayload(t *testing.T) {
// 无 fields
err := xerror.NewXCode(xcode.InternalServerError, "变更消息")
payload := xerror.ParsePayload(err)
if payload != nil {
t.Fatalf("expected nil payload, got %v", payload)
}
// 有 fields
err2 := xerror.Wrap(err, "错误2").
WithFields(xerror.F("name", "abc")).
WithFields(
xerror.F("k", "v"),
xerror.F("a", 1),
)
payload2 := xerror.ParsePayload(err2)
if payload2 == nil {
t.Fatal("expected non-nil payload")
}
if payload2["name"] != "abc" {
t.Fatalf("expected name=abc, got %v", payload2["name"])
}
plainErr := errors.New("plain")
if xerror.ParsePayload(plainErr) != nil {
t.Fatal("plain error should return nil")
}
}
func TestFormatStackTrace(t *testing.T) {
err := xerror.NewXCode(xcode.InternalServerError, "变更消息")
trace := fmt.Sprintf("%+v", err)
if len(trace) == 0 {
t.Fatal("FormatStackTrace should not be empty")
}
}
func TestStackTrace(t *testing.T) {
err := xerror.NewCode(403, "forbidden")
trace := xerror.StackTrace(err)
if len(trace) == 0 {
t.Fatal("StackTrace should not be empty for XError")
}
}
func TestStackTrace_PlainError(t *testing.T) {
err := errors.New("plain")
trace := xerror.StackTrace(err)
if trace != "" {
t.Fatalf("plain error should have empty stack trace, got %q", trace)
}
}
func TestFormatMsg(t *testing.T) {
err := xerror.NewCode(100001, "未登录")
msg := xerror.FormatMsg(err)
if msg != "[100001] 未登录" {
t.Fatalf("expected '[100001] 未登录', got %q", msg)
}
// 非 XError
plainErr := error(newPlainError("plain"))
msg2 := xerror.FormatMsg(plainErr)
if msg2 != "plain" {
t.Fatalf("expected 'plain', got %q", msg2)
}
}
type plainError string
func (e plainError) Error() string { return string(e) }
func newPlainError(s string) error { return plainError(s) }
type testCustomCode struct {
httpStatus int
code int
message string
}
func (c *testCustomCode) Code() int { return c.code }
func (c *testCustomCode) HttpStatus() int { return c.httpStatus }
func (c *testCustomCode) String() string { return c.message }
func TestWrapPreservesHTTPStatus(t *testing.T) {
// 仓库中的 code
err1 := xerror.NewXCode(xcode.RequestParamError) // httpStatus=400
werr1 := xerror.Wrap(err1, "wrapped")
if werr1.HttpStatus() != 400 {
t.Fatalf("expected httpStatus 400 for repo code, got %d", werr1.HttpStatus())
}
// 仓库中不存在的 code,但原始 XCode 有自定义 httpStatus
cc := &testCustomCode{httpStatus: 400, code: 999999, message: "自定义错误"}
err2 := xerror.NewXCode(cc)
werr2 := xerror.Wrap(err2, "wrapped")
if werr2.HttpStatus() != 400 {
t.Fatalf("expected httpStatus 400 for custom code, got %d", werr2.HttpStatus())
}
// Wrap 保留原始 code
if werr2.ErrorCode() != 999999 {
t.Fatalf("expected code 999999, got %d", werr2.ErrorCode())
}
// Wrap 保留原始 httpStatus 但更新消息
if werr2.Message() != "wrapped" {
t.Fatalf("expected Message() 'wrapped', got %q", werr2.Message())
}
}
func TestWrap_OverridePropagation(t *testing.T) {
// 先创建错误
err := xerror.NewXCode(xcode.Forbidden) // code=403, httpStatus=403
// Override 改变 httpStatus
xcode.Override(403, http.StatusMethodNotAllowed, "覆写403")
defer xcode.Reset()
// Wrap 后应保留原始 XError 的 httpStatus(即 Override 前的 403),
// 而不是使用 Override 后的 httpStatus
wrapped := xerror.Wrap(err, "wrapped")
if wrapped.HttpStatus() != http.StatusForbidden {
t.Fatalf("expected original httpStatus %d, got %d", http.StatusForbidden, wrapped.HttpStatus())
}
if wrapped.ErrorCode() != 403 {
t.Fatalf("expected code 403, got %d", wrapped.ErrorCode())
}
}
func TestWrapPreservesErrorChain(t *testing.T) {
inner := xerror.NewXCode(xcode.RequestParamError) // code=400
wrapped := xerror.Wrap(inner, "wrapped")
// Unwrap 后应能找到原始 XError
unwrapped := wrapped.Unwrap()
var found xerror.XError
if !errors.As(unwrapped, &found) {
t.Fatal("expected to find XError in unwrap chain")
}
if found.ErrorCode() != 400 {
t.Fatalf("expected unwrapped XError code 400, got %d", found.ErrorCode())
}
// IsErrorCode 应能找到内部 code
if !xerror.IsErrorCode(wrapped, 400) {
t.Fatal("expected IsErrorCode to find code 400 in chain")
}
}
func TestNewDefaultCode(t *testing.T) {
err := xerror.New("简单错误")
if err.ErrorCode() != xcode.CodeNone {
t.Fatalf("New() should use CodeNone(%d), got %d", xcode.CodeNone, err.ErrorCode())
}
if err.HttpStatus() != 500 {
t.Fatalf("New() httpStatus should be 500, got %d", err.HttpStatus())
}
if err.Message() != "简单错误" {
t.Fatalf("New() Message() should be '简单错误', got %q", err.Message())
}
}
func TestIs_Semantic(t *testing.T) {
// 同 code:应匹配(同码即同类,标准 errors.Is 语义)
err1 := xerror.NewCode(1001, "数据库错误")
err2 := xerror.NewCode(1001, "Database error")
if !err1.Is(err2) {
t.Fatal("same code should be Is-equal (kind semantics)")
}
// 不同 code:不应匹配
err3 := xerror.NewCode(1001, "数据库错误")
err4 := xerror.NewCode(1002, "数据库错误")
if err3.Is(err4) {
t.Fatal("different code should not be Is-equal")
}
// CodeNone:不同实例不匹配(即使消息相同)
err5 := xerror.New("数据库连接失败")
err6 := xerror.New("数据库连接失败")
if err5.Is(err6) {
t.Fatal("CodeNone XErrors should not be Is-equal (independent errors)")
}
// CodeNone 与自身匹配
if !err5.Is(err5) {
t.Fatal("CodeNone XError should match itself")
}
// CodeNone + 不同消息:不应匹配
err7 := xerror.New("数据库连接失败")
err8 := xerror.New("用户不存在")
if err7.Is(err8) {
t.Fatal("CodeNone with different message should not be Is-equal")
}
// CodeNone vs 有业务码:不应匹配
err9 := xerror.New("错误")
err10 := xerror.NewCode(1001, "错误")
if err9.Is(err10) {
t.Fatal("CodeNone vs non-CodeNone should not be Is-equal")
}
}
func TestWrapWithCodePreservesMessage(t *testing.T) {
err := errors.New("原始错误")
werr := xerror.WrapWithCode(err, 3333)
if werr.Message() != "原始错误" {
t.Fatalf("expected Message() '原始错误', got %q", werr.Message())
}
if werr.ErrorCode() != 3333 {
t.Fatalf("expected code 3333, got %d", werr.ErrorCode())
}
}
func TestFormatConsistency(t *testing.T) {
err := xerror.NewCode(1001, "数据库错误")
// %v 应等同于 Error()
if fmt.Sprintf("%v", err) != err.Error() {
t.Fatalf("%%v should equal Error(): got %q vs %q", fmt.Sprintf("%v", err), err.Error())
}
// %s 应等同于 Error()
if fmt.Sprintf("%s", err) != err.Error() {
t.Fatalf("%%s should equal Error(): got %q vs %q", fmt.Sprintf("%s", err), err.Error())
}
// %+v 应包含栈信息
detailed := fmt.Sprintf("%+v", err)
if len(detailed) == 0 {
t.Fatalf("%%+v should not be empty")
}
// FormatMsg 应包含 [code] 前缀
msg := xerror.FormatMsg(err)
if msg != "[1001] 数据库错误" {
t.Fatalf("FormatMsg should be '[1001] 数据库错误', got %q", msg)
}
}
func TestMessage(t *testing.T) {
// 单层错误:Message() == Error()
err := xerror.New("简单错误")
if err.Message() != "简单错误" {
t.Fatalf("expected Message() '简单错误', got %q", err.Message())
}
if err.Message() != err.Error() {
t.Fatalf("Message() should equal Error() for single-layer error")
}
// 包装后:Message() != Error()
inner := xerror.New("原始错误")
wrapped := xerror.Wrap(inner, "包装错误")
if wrapped.Message() != "包装错误" {
t.Fatalf("expected Message() '包装错误', got %q", wrapped.Message())
}
// Error() 包含完整链
if wrapped.Error() == "包装错误" {
t.Fatal("Error() should contain chain context, not just Message()")
}
// NewCode 的 Message()
codeErr := xerror.NewCode(1001, "数据库错误")
if codeErr.Message() != "数据库错误" {
t.Fatalf("expected Message() '数据库错误', got %q", codeErr.Message())
}
// nil code 降级
xcErr := xerror.NewXCode(nil) // 降级为 InternalServerError
if xcErr.Message() != xcode.InternalServerError.String() {
t.Fatalf("expected Message() fallback to InternalServerError, got %q", xcErr.Message())
}
}
func TestErrorf_WithoutPercentW(t *testing.T) {
err := xerror.Errorf("simple %s", "error")
if err.Message() != "simple error" {
t.Fatalf("expected 'simple error', got %q", err.Message())
}
}
func TestNewXCodef(t *testing.T) {
err := xerror.NewXCodef(xcode.InternalServerError, "user %s not found, code: %d", "test", 404)
if err.Message() != "user test not found, code: 404" {
t.Fatalf("expected 'user test not found, code: 404', got %q", err.Message())
}
if err.ErrorCode() != xcode.InternalServerError.Code() {
t.Fatalf("expected code %d, got %d", xcode.InternalServerError.Code(), err.ErrorCode())
}
}