-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_xcode_test.go
More file actions
92 lines (76 loc) · 2.19 KB
/
Copy pathload_xcode_test.go
File metadata and controls
92 lines (76 loc) · 2.19 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
package examples
import (
"fmt"
"net/http"
"github.com/save95/xerror"
"github.com/save95/xerror/xcode"
)
type xCode struct {
httpStatus int
code int
message string
}
func (c xCode) Code() int {
return c.code
}
func (c xCode) HttpStatus() int {
return c.httpStatus
}
func (c xCode) String() string {
return c.message
}
func ExampleLoadCodes() {
var (
unauthorized = xCode{httpStatus: http.StatusInternalServerError, code: 401, message: "overwrite 请求未授权"}
forbidden = xCode{httpStatus: http.StatusForbidden, code: 403, message: "overwrite 无权访问"}
)
var allCode = []xcode.XCode{
unauthorized,
forbidden,
}
// 加载自定义的错误码列表,会通过 xcode.XCode.Code() 值进行覆写
xerror.LoadCodes(allCode...)
// 系统错误码的消息
fmt.Println(xcode.Unauthorized.String())
fmt.Println(xcode.Forbidden.String())
// 错误消息被覆写
fmt.Println(xerror.WithCode(unauthorized.code, unauthorized.message))
fmt.Println(xerror.WithCode(forbidden.code, forbidden.message))
// HTTP 状态码被覆写
fmt.Println(xerror.WithCode(unauthorized.code, "overwrite").HttpStatus())
// Output:
// 请求未授权
// 无权访问
// overwrite 请求未授权
// overwrite 无权访问
// 500
}
func ExampleAppendCodes() {
var (
E403 = xCode{httpStatus: http.StatusInternalServerError, code: 403, message: "overwrite 403 error"}
E1001 = xCode{httpStatus: http.StatusUnauthorized, code: 1001, message: "1001 error"}
)
// 追加系统错误码列表;如果已存在,则直接跳过
xerror.AppendCodes(E403, E1001)
// 追加后,可以直接使用
fmt.Println(xerror.WithXCode(E403))
fmt.Println(xerror.WithXCode(E403).HttpStatus())
fmt.Println(xerror.WithXCode(E1001))
// 对于已存在的错误码,无法被覆写
// 这时,调用 xerror.WithCode 所使用的为系统定义的错误码
err := xerror.WithCode(E403.code, "with message")
fmt.Println(err)
fmt.Println(err.ErrorCode() == E403.code)
fmt.Println(err.HttpStatus() != E403.httpStatus)
fmt.Println(err.String())
fmt.Println(err.String() != E403.message)
// Output:
// overwrite 403 error
// 500
// 1001 error
// with message
// true
// true
// with message
// true
}