-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_usage_test.go
More file actions
83 lines (67 loc) · 1.66 KB
/
Copy pathbase_usage_test.go
File metadata and controls
83 lines (67 loc) · 1.66 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
package examples
import (
"fmt"
"github.com/save95/xerror"
"github.com/save95/xerror/xcode"
)
func ExampleNew() {
err := xerror.New("whoops")
fmt.Println(err)
fmt.Println(err.HttpStatus())
fmt.Println(err.ErrorCode())
// Output: whoops
// 500
// 500
}
func ExampleWithCode() {
code := 1001
err := xerror.WithCode(code, "server error message")
fmt.Println(err)
fmt.Println(err.HttpStatus())
fmt.Println(err.ErrorCode())
// Output:
// server error message
// 500
// 1001
}
func ExampleWithCode_reserveCode() {
code := 500
err := xerror.WithCode(code, "customize server error message")
// 使用系统保留错误码,只替换错误码消息
fmt.Println(err)
// HTTP 状态码和错误码不会被覆写
fmt.Println(err.HttpStatus())
fmt.Println(err.ErrorCode())
// 错误消息被覆写
fmt.Println(err.String() != xcode.InternalServerError.String())
// Output:
// customize server error message
// 500
// 500
// true
}
func ExampleWithCodef() {
code := 1001
err := xerror.WithCodef(code, "format error: code[%d] message: %s", code, "some error")
fmt.Println(err)
// Output:
// format error: code[1001] message: some error
}
func ExampleWithXCode() {
err := xerror.WithXCode(xcode.InternalServerError)
fmt.Println(err)
// Output:
// 内部服务错误
}
func ExampleWithXCodeMessage() {
err := xerror.WithXCodeMessage(xcode.InternalServerError, "customize error message")
fmt.Println(err)
// Output:
// customize error message
}
func ExampleWithXCodeMessagef() {
err := xerror.WithXCodeMessagef(xcode.InternalServerError, "format error message: %s", "some error")
fmt.Println(err)
// Output:
// format error message: some error
}