-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_usage_test.go
138 lines (119 loc) · 2.65 KB
/
error_usage_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
package errors
import (
"fmt"
"testing"
)
var myerror = New("name is empty")
func Service(name string) error {
if name == "" {
return myerror
}
return nil
}
func Controller() error {
err := Service("")
if err != nil {
return Wrap(err, "Service has error")
}
return nil
}
func funcA() error {
if err := funcB(); err != nil {
return Wrap(err, "call funcB failed")
}
return New("func called error")
}
func funcB() error {
return New("func called error")
}
// 自定义的错误类型
type DefineError struct {
msg string
}
func (d *DefineError) Error() string {
return d.msg
}
func Service1(name string) error {
if name == "" {
return &DefineError{msg: "error 1"}
}
return nil
}
func Controller1() error {
err := Service1("")
if err != nil {
return Wrap(err, "Service has error")
}
return nil
}
/*
测试Wrap 函数
=== RUN TestWrap
error_user_test.go:21: call funcB failed
=============
error_user_test.go:25: func called error
logtest.funcB
/Users/maozhongyu/code/logtest/error_user_test.go:17
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:10
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
call funcB failed
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:11
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
--- PASS: TestWrap (0.00s)
PASS
*/
func TestWrap(t *testing.T) {
t.Logf("%v", funcA())
fmt.Println("=============")
t.Logf("%+v", funcA())
}
/*
测试 Is函数
一般用在哨兵错误
=== RUN TestIs
is myerror value
--- PASS: TestIs (0.00s)
PASS
*/
func TestIs(t *testing.T) {
err := Controller()
if Is(err, myerror) {
fmt.Println("is myerror value") // 输出
}
}
/*
测试 As函数
error 1
Service has error
logtest.Controller1
/Users/maozhongyu/code/logtest/error_user_test.go:56
logtest.TestAs
/Users/maozhongyu/code/logtest/error_user_test.go:116
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
-----------
AS error
*/
func TestAs(t *testing.T) {
err := Controller1()
fmt.Printf("%+v\n", err)
fmt.Println("-----------")
var myerror *DefineError
if As(err, &myerror) {
fmt.Println("AS error")
}
}