-
Notifications
You must be signed in to change notification settings - Fork 0
/
errno.go
80 lines (66 loc) · 1.48 KB
/
errno.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
package errno
import (
"encoding/json"
"github.com/pkg/errors"
)
var _ Error = (*err)(nil)
type Error interface {
// i 为了避免被其他包实现
i()
// WithErr 设置错误信息
WithErr(err error) Error
// GetBusinessCode 获取 Business Code
GetBusinessCode() int
// GetHttpCode 获取 HTTP Code
GetHttpCode() int
// GetMsg 获取 Msg
GetMsg() string
// GetErr 获取错误信息
GetErr() error
// ToString 返回 JSON 格式的错误详情
ToString() string
}
type err struct {
HttpCode int // HTTP Code
BusinessCode int // Business Code
Message string // 描述信息
Err error // 错误信息
}
func NewError(httpCode, businessCode int, msg string) Error {
return &err{
HttpCode: httpCode,
BusinessCode: businessCode,
Message: msg,
}
}
func (e *err) i() {}
func (e *err) WithErr(err error) Error {
e.Err = errors.WithStack(err)
return e
}
func (e *err) GetHttpCode() int {
return e.HttpCode
}
func (e *err) GetBusinessCode() int {
return e.BusinessCode
}
func (e *err) GetMsg() string {
return e.Message
}
func (e *err) GetErr() error {
return e.Err
}
// ToString 返回 JSON 格式的错误详情
func (e *err) ToString() string {
err := &struct {
HttpCode int `json:"http_code"`
BusinessCode int `json:"business_code"`
Message string `json:"message"`
}{
HttpCode: e.HttpCode,
BusinessCode: e.BusinessCode,
Message: e.Message,
}
raw, _ := json.Marshal(err)
return string(raw)
}