-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_test.go
More file actions
102 lines (83 loc) · 1.98 KB
/
wrap_test.go
File metadata and controls
102 lines (83 loc) · 1.98 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
package examples
import (
"errors"
"fmt"
"github.com/save95/xerror"
"github.com/save95/xerror/xcode"
)
func ExampleWrap() {
err := errors.New("first error")
werr := xerror.Wrap(err, "second error")
fmt.Println(werr)
// Output:
// second error: first error
}
func ExampleWrap_xerror() {
err := xerror.New("first xerror")
werr := xerror.Wrap(err, "second xerror")
fmt.Println(werr)
fmt.Println(werr.ErrorCode() == err.ErrorCode())
fmt.Println(werr.HttpStatus() == err.HttpStatus())
// Output:
// second xerror: first xerror
// true
// true
}
func ExampleWrapWithXCode() {
err := xerror.New("gorm failed")
werr := xerror.WrapWithXCode(err, xcode.DBFailed)
fmt.Println(werr)
fmt.Println(werr.ErrorCode() != err.ErrorCode())
fmt.Println(werr.HttpStatus())
// Output:
// 数据库操作失败: gorm failed
// true
// 500
}
func ExampleWrapWithXCodeStatus() {
err := xerror.New("gorm failed")
werr := xerror.WrapWithXCodeStatus(err, xcode.RequestParamError)
fmt.Println(werr)
fmt.Println(werr.ErrorCode() != err.ErrorCode())
fmt.Println(err.HttpStatus())
fmt.Println(werr.HttpStatus())
// Output:
// gorm failed
// true
// 500
// 400
}
func ExampleWrapWithCode() {
code := 3333
err := xerror.New("first xerror")
werr := xerror.WrapWithCode(err, code)
fmt.Println(err.ErrorCode() != code)
fmt.Println(werr)
fmt.Println(werr.ErrorCode() == code)
fmt.Println(werr.ErrorCode() != err.ErrorCode())
fmt.Println(werr.HttpStatus())
// Output:
// true
// 内部服务错误: first xerror
// true
// true
// 500
}
func ExampleWrapWithCode_reserveCode() {
code := 401
err := xerror.New("first xerror")
werr := xerror.WrapWithCode(err, code)
fmt.Println(err.ErrorCode() != code)
fmt.Println(werr)
fmt.Println(werr.ErrorCode() == code)
fmt.Println(werr.ErrorCode() == xcode.Unauthorized.Code())
fmt.Println(werr.ErrorCode() != err.ErrorCode())
fmt.Println(werr.HttpStatus())
// Output:
// true
// 内部服务错误: first xerror
// true
// true
// true
// 401
}