-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_test.go
More file actions
179 lines (147 loc) · 4.79 KB
/
Copy pathwrap_test.go
File metadata and controls
179 lines (147 loc) · 4.79 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
package xerror
import (
"fmt"
"net/http"
"testing"
"github.com/gomooth/xerror/xcode"
)
func TestWrap(t *testing.T) {
xc := xcode.NewWithMessage(10001, "某种内容错误")
xe := NewXCode(xc)
wxe := Wrap(xe, "wrap test")
if wxe.ErrorCode() != 10001 {
t.Fatalf("expected code 10001, got %d", wxe.ErrorCode())
}
if wxe.Message() != "wrap test" {
t.Fatalf("expected 'wrap test', got %q", wxe.Message())
}
// Wrap nil 返回 nil
if Wrap(nil, "test") != nil {
t.Fatal("Wrap(nil, ...) should return nil")
}
}
func TestWrap_Field(t *testing.T) {
err := New("field error").
WithFields(F("key1", "value1")).
WithFields(F("append", "field"))
wxe := Wrap(err, "wrap test")
fields := wxe.GetFields()
if len(fields) != 2 {
t.Fatalf("expected 2 fields, got %d", len(fields))
}
}
func TestWrap_Trace(t *testing.T) {
var a1 = func() error {
return Errorf("origin error")
}
var a2 = func() error {
if err := a1(); nil != err {
return Wrap(err, "wrap a2 error")
}
return nil
}
var a3 = func() error {
if err := a2(); nil != err {
return Wrap(err, "wrap a3 error")
}
return nil
}
res := a3()
trace := fmt.Sprintf("%+v", res)
if len(trace) == 0 {
t.Fatal("stack trace should not be empty")
}
}
func TestWrapWithXCode(t *testing.T) {
err := New("base error")
wrapped := WrapWithXCode(err, xcode.Forbidden)
if wrapped.HttpStatus() != xcode.Forbidden.HttpStatus() {
t.Fatalf("expected httpStatus %d, got %d", xcode.Forbidden.HttpStatus(), wrapped.HttpStatus())
}
if wrapped.ErrorCode() != xcode.Forbidden.Code() {
t.Fatalf("expected code %d, got %d", xcode.Forbidden.Code(), wrapped.ErrorCode())
}
// nil code 应默认 InternalServerError
wrapped2 := WrapWithXCode(err, nil)
if wrapped2.ErrorCode() != xcode.InternalServerError.Code() {
t.Fatalf("nil code should default to InternalServerError")
}
// nil error 返回 nil
if WrapWithXCode(nil, xcode.Forbidden) != nil {
t.Fatal("WrapWithXCode(nil, ...) should return nil")
}
}
func TestWrap_PreservesOriginalHttpStatus_WhenOverrideExists(t *testing.T) {
// 先 Override 404 为 403
xcode.Override(404, http.StatusForbidden, "自定义404")
// 用 404 XCode 创建 XError
// ResourceNotFound 是全局变量,Override 不改变该变量,因此 httpStatus 仍为 404
inner := NewXCode(xcode.ResourceNotFound) // httpStatus=404
// Wrap 该错误,应保留原始 XError 的 httpStatus(即 404,而非 Override 后的 403)
wrapped := Wrap(inner, "wrapped message")
if wrapped.HttpStatus() != http.StatusNotFound {
t.Fatalf("expected httpStatus 404 (from original XError), got %d", wrapped.HttpStatus())
}
if wrapped.ErrorCode() != 404 {
t.Fatalf("expected code 404, got %d", wrapped.ErrorCode())
}
if wrapped.Message() != "wrapped message" {
t.Fatalf("expected 'wrapped message', got %q", wrapped.Message())
}
// Reset 恢复
xcode.Reset()
}
func TestWrapWithCode_Nil(t *testing.T) {
if WrapWithCode(nil, 403) != nil {
t.Fatal("WrapWithCode(nil, ...) should return nil")
}
}
func TestWrapStatus(t *testing.T) {
err := New("base error")
wrapped := WrapStatus(err, xcode.Forbidden)
if wrapped.HttpStatus() != xcode.Forbidden.HttpStatus() {
t.Fatalf("expected httpStatus %d, got %d", xcode.Forbidden.HttpStatus(), wrapped.HttpStatus())
}
// 消息应来自原始 error
if wrapped.Message() != "base error" {
t.Fatalf("expected 'base error', got %q", wrapped.Message())
}
}
func TestWrapSanitize(t *testing.T) {
inner := New("base error").WithFields(F("userId", 123), F("sql", "SELECT * FROM users"))
// Wrap 继承 fields
wrapped := Wrap(inner, "包装错误")
if len(wrapped.GetFields()) != 2 {
t.Fatalf("Wrap should inherit 2 fields, got %d", len(wrapped.GetFields()))
}
// WrapSanitize 不继承 fields
cleaned := WrapSanitize(inner, "包装错误")
if len(cleaned.GetFields()) != 0 {
t.Fatalf("WrapSanitize should have no fields, got %d", len(cleaned.GetFields()))
}
// WrapSanitize 保留 code/httpStatus
if cleaned.ErrorCode() != inner.ErrorCode() {
t.Fatalf("WrapSanitize should preserve code, got %d", cleaned.ErrorCode())
}
if cleaned.HttpStatus() != inner.HttpStatus() {
t.Fatalf("WrapSanitize should preserve httpStatus, got %d", cleaned.HttpStatus())
}
// WrapSanitize 替换消息
if cleaned.Message() != "包装错误" {
t.Fatalf("expected '包装错误', got %q", cleaned.Message())
}
// WrapSanitize nil 返回 nil
if WrapSanitize(nil, "test") != nil {
t.Fatal("WrapSanitize(nil, ...) should return nil")
}
}
func TestWrapSanitize_PlainError(t *testing.T) {
plainErr := fmt.Errorf("plain error")
cleaned := WrapSanitize(plainErr, "cleaned")
if cleaned.Message() != "cleaned" {
t.Fatalf("expected 'cleaned', got %q", cleaned.Message())
}
if len(cleaned.GetFields()) != 0 {
t.Fatalf("WrapSanitize(plain) should have no fields, got %d", len(cleaned.GetFields()))
}
}