-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_query_test.go
More file actions
137 lines (121 loc) · 3.64 KB
/
Copy pathfunc_query_test.go
File metadata and controls
137 lines (121 loc) · 3.64 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
package xerror_test
import (
"errors"
"testing"
"github.com/gomooth/xerror"
"github.com/gomooth/xerror/xcode"
)
func TestCause(t *testing.T) {
// 单层 XError:无 cause
err := xerror.New("base")
if xerror.Cause(err) != nil {
t.Fatal("single XError should have no cause")
}
// 两层 Wrap:Cause 返回业务层内部错误
inner := xerror.NewCode(100001, "内部错误")
wrapped := xerror.Wrap(inner, "外部错误")
cause := xerror.Cause(wrapped)
if cause == nil {
t.Fatal("wrapped error should have a cause")
}
// Cause 返回 error,需用 AsXError 提取 XError
xe, ok := xerror.AsXError(cause)
if !ok {
t.Fatal("cause should be extractable as XError")
}
if xe.ErrorCode() != 100001 {
t.Fatalf("cause should have code 100001, got %d", xe.ErrorCode())
}
// 非 XError 输入
if xerror.Cause(errors.New("plain")) != nil {
t.Fatal("plain error should return nil cause")
}
// 混合 error 链:XError 包装标准 error
plainErr := errors.New("plain inner")
wrappedPlain := xerror.Wrap(plainErr, "wrap plain")
cause2 := xerror.Cause(wrappedPlain)
if cause2 == nil {
t.Fatal("wrapped plain error should have a cause")
}
if cause2.Error() != "plain inner" {
t.Fatalf("cause should be 'plain inner', got %q", cause2.Error())
}
// WrapStatus:直接存储原始 error,Cause 应正确返回
wrappedStatus := xerror.WrapStatus(inner, xcode.Forbidden)
cause3 := xerror.Cause(wrappedStatus)
if cause3 == nil {
t.Fatal("WrapStatus wrapped error should have a cause")
}
xe3, ok3 := xerror.AsXError(cause3)
if !ok3 {
t.Fatal("cause of WrapStatus should be extractable as XError")
}
if xe3.ErrorCode() != 100001 {
t.Fatalf("cause should have code 100001, got %d", xe3.ErrorCode())
}
}
func TestRootCause(t *testing.T) {
// 单层 XError:root 就是 Unwrap 链的最底层
err := xerror.New("base")
root := xerror.RootCause(err)
if root == nil {
t.Fatal("single XError should have root cause")
}
// 多层 Wrap
inner := xerror.NewCode(100001, "内部错误")
wrapped := xerror.Wrap(inner, "外部错误")
doubleWrapped := xerror.Wrap(wrapped, "最外层")
root = xerror.RootCause(doubleWrapped)
if root == nil {
t.Fatal("should have root cause")
}
// RootCause 返回 error,用 AsXError 提取
xe, ok := xerror.AsXError(root)
if !ok {
t.Fatal("root cause should be extractable as XError in pure XError chain")
}
if xe.ErrorCode() != 100001 {
t.Fatalf("root cause should have code 100001, got %d", xe.ErrorCode())
}
// 非 XError 输入
plainRoot := xerror.RootCause(errors.New("plain"))
if plainRoot == nil {
t.Fatal("plain error should return itself as root")
}
if plainRoot.Error() != "plain" {
t.Fatalf("plain root should be 'plain', got %q", plainRoot.Error())
}
}
func TestRootCause_JoinedError(t *testing.T) {
inner := xerror.NewCode(100001, "根因")
joined := errors.Join(inner, errors.New("other"))
root := xerror.RootCause(joined)
if root == nil {
t.Fatal("RootCause should find root in joined error chain")
}
}
func TestAsXError(t *testing.T) {
// XError 输入
err := xerror.NewCode(403, "forbidden")
xe, ok := xerror.AsXError(err)
if !ok {
t.Fatal("AsXError should extract XError")
}
if xe.ErrorCode() != 403 {
t.Fatalf("expected code 403, got %d", xe.ErrorCode())
}
// 标准 error 输入
_, ok = xerror.AsXError(errors.New("plain"))
if ok {
t.Fatal("AsXError should return false for plain error")
}
// 包装后的 XError 也能提取
wrapped := xerror.Wrap(err, "wrapped")
xe2, ok2 := xerror.AsXError(wrapped)
if !ok2 {
t.Fatal("AsXError should extract from wrapped XError")
}
if xe2.ErrorCode() != 403 {
t.Fatalf("expected code 403, got %d", xe2.ErrorCode())
}
}