1
1
# xerror
2
2
3
- go error 简单实现
3
+ > go error 简单实现
4
+
5
+ 1 . 高效处理golang的error, 避免处理大量的 ` err!=nil ` 判断
6
+ 2 . 高效处理golang的recover, 让错误中包含丰富的堆栈信息
7
+ 3 . xerror实现标准As,Is,Unwrap接口, 可以和其他error库一起使用
8
+ 4 . 简单易用
4
9
5
10
6
11
## 性能分析
@@ -22,105 +27,51 @@ ok github.com/pubgo/xerror 4.363s
22
27
```
23
28
24
29
## example
25
- ``` go
26
- package xerror_http
27
-
28
- import (
29
- " github.com/pubgo/xerror"
30
- " net/http"
31
- )
32
-
33
- var (
34
- ErrHttp = xerror.New (" http error" , " http错误" )
35
- ErrBadRequest = ErrHttp .New (" 400" , http.StatusText (400 ))
36
- ErrUnauthorized = ErrHttp .New (" 401" , http.StatusText (401 ))
37
- ErrForbidden = ErrHttp .New (" 403" , http.StatusText (403 ))
38
- ErrNotFound = ErrHttp .New (" 404" , http.StatusText (404 ))
39
- ErrMethodNotAllowed = ErrHttp .New (" 405" , http.StatusText (405 ))
40
- ErrTimeout = ErrHttp .New (" 408" , http.StatusText (408 ))
41
- ErrConflict = ErrHttp .New (" 409" , http.StatusText (409 ))
42
- ErrInternalServerError = ErrHttp .New (" 500" , http.StatusText (500 ))
43
- )
44
- ```
45
-
46
30
``` go
47
31
package xerror_test
48
32
49
33
import (
50
34
" fmt"
51
- " log"
52
35
" testing"
53
36
54
37
" github.com/pubgo/xerror"
55
- " github.com/pubgo/xerror/xerror_core"
56
- " github.com/pubgo/xerror/xerror_http"
57
38
)
58
39
59
- func check (b bool ) {
60
- if !b {
61
- log.Fatalln (" " )
62
- }
63
- }
64
-
65
- func panic1 (a ...interface {}) (err error ) {
66
- defer xerror.RespErr (&err)
67
- xerror.PanicF (xerror_http.ErrBadRequest , " panic1 %+v " , a)
68
- return
69
- }
70
-
71
- func panic2 (a ...interface {}) (err error ) {
72
- defer xerror.RespErr (&err)
73
- xerror.PanicF (panic1 (a...), " panic2 %+v " , a)
74
- return
75
- }
76
-
77
- func panicWrap (a ...interface {}) (err error ) {
78
- return xerror.WrapF (panic2 (a...), " panicWrap %+v " , a)
40
+ func TestErr (t *testing .T ) {
41
+ fmt.Println (xerror.Wrap (xerror.ErrAssert ))
79
42
}
80
43
81
- func TestStack (t *testing .T ) {
82
- defer xerror.Resp (func (err xerror.XErr ) {
83
- fmt.Println (err.Stack (true ))
44
+ func TestParseWith (t *testing .T ) {
45
+ var err = fmt.Errorf (" hello error" )
46
+ xerror.ParseWith (err, func (err xerror.XErr ) {
47
+ fmt.Printf (" %v \n " , err)
84
48
})
85
- xerror.Panic (panicWrap (1 , 2 , 4 , 5 ))
86
49
}
87
50
88
- func TestAs (t *testing .T ) {
89
- check (xerror.FamilyAs (panicWrap (1 , 2 , 4 , 5 ), xerror_http.ErrHttp ) == true )
90
- check (xerror.FamilyAs (panicWrap (1 , 2 , 4 , 5 ), xerror_http.ErrBadRequest ) == true )
91
- check (xerror.FamilyAs (panicWrap (1 , 2 , 4 , 5 ), xerror_http.ErrNotFound ) == false )
51
+ func TestRespTest (t *testing .T ) {
52
+ defer xerror.RespTest (t)
53
+ TestPanic1 (t)
92
54
}
93
55
94
- func TestExit (t *testing .T ) {
95
- xerror_core. PrintStack = false
96
- xerror. Exit ( panicWrap ( 1 , 2 , 4 , 5 ) )
56
+ func TestRespNext (t *testing .T ) {
57
+ defer xerror. RespExit ( " TestRespNext " )
58
+ TestPanic1 (t )
97
59
}
98
60
99
- func TestTry (t *testing .T ) {
100
- fmt. Println ( xerror.Try ( func () {
101
- panic ( " hello " )
102
- }) )
103
- }
61
+ func TestPanic1 (t *testing .T ) {
62
+ // defer xerror.RespExit()
63
+ defer xerror. RespRaise ( func (err xerror. XErr ) error {
64
+ return xerror. WrapF (err, " test raise " )
65
+ })
104
66
105
- func BenchmarkPanic (b *testing .B ) {
106
- for i := 0 ; i < b.N ; i++ {
107
- _ = func () (err error ) {
108
- defer xerror.RespErr (&err)
109
- xerror.Panic (xerror_http.ErrBadRequest )
110
- return
111
- }()
112
- }
67
+ // xerror.Panic(xerror.New("ok"))
68
+ xerror.Panic (fmt.Errorf (" ss" ))
113
69
}
114
70
115
- func BenchmarkPanicWithoutCaller (b *testing .B ) {
116
- xerror_core.IsCaller = false
117
- for i := 0 ; i < b.N ; i++ {
118
- _ = func () (err error ) {
119
- defer xerror.RespErr (&err)
120
- xerror.Panic (xerror_http.ErrBadRequest )
121
- return
122
- }()
123
- }
71
+ func init1Next () (err error ) {
72
+ defer xerror.RespErr (&err)
73
+ xerror.Panic (fmt.Errorf (" test next" ))
74
+ return nil
124
75
}
125
76
126
77
func BenchmarkNoPanic (b *testing .B ) {
0 commit comments