|
@@ -4,11 +4,26 @@ import ( |
|
@@ -4,11 +4,26 @@ import ( |
4
|
"encoding/json"
|
4
|
"encoding/json"
|
5
|
)
|
5
|
)
|
6
|
|
6
|
|
|
|
7
|
+//CustomErrParse 解析自定义错误结构体
|
7
|
type CustomErrParse interface {
|
8
|
type CustomErrParse interface {
|
8
|
- ParseToMessage() interface{}
|
9
|
+ ParseToMessage() *ResponseMessage
|
9
|
}
|
10
|
}
|
10
|
|
11
|
|
11
|
-//ErrorCode 统一错误编码结构
|
12
|
+//ErrorMap 统一消息错误编码
|
|
|
13
|
+type ErrorMap map[int]string
|
|
|
14
|
+
|
|
|
15
|
+//Search 搜索错误描述
|
|
|
16
|
+func (m ErrorMap) Search(code int) ErrorCode {
|
|
|
17
|
+ if v, ok := m[code]; ok {
|
|
|
18
|
+ return ErrorCode{
|
|
|
19
|
+ Errno: code,
|
|
|
20
|
+ Errmsg: v,
|
|
|
21
|
+ }
|
|
|
22
|
+ }
|
|
|
23
|
+ return ErrorCode{}
|
|
|
24
|
+}
|
|
|
25
|
+
|
|
|
26
|
+//ErrorCode 统一错误结构
|
12
|
type ErrorCode struct {
|
27
|
type ErrorCode struct {
|
13
|
Errno int `json:"code"`
|
28
|
Errno int `json:"code"`
|
14
|
Errmsg string `json:"msg"`
|
29
|
Errmsg string `json:"msg"`
|
|
@@ -22,22 +37,27 @@ type ResponseMessage struct { |
|
@@ -22,22 +37,27 @@ type ResponseMessage struct { |
22
|
|
37
|
|
23
|
func NewMesage(code int) *ResponseMessage {
|
38
|
func NewMesage(code int) *ResponseMessage {
|
24
|
return &ResponseMessage{
|
39
|
return &ResponseMessage{
|
25
|
- ErrorCode: errmessge.Search(code),
|
40
|
+ ErrorCode: SearchErr(code),
|
26
|
Data: nil,
|
41
|
Data: nil,
|
27
|
}
|
42
|
}
|
28
|
}
|
43
|
}
|
29
|
|
44
|
|
30
|
//ErrWithMessage 自定义错误结构
|
45
|
//ErrWithMessage 自定义错误结构
|
31
|
type ErrWithMessage struct {
|
46
|
type ErrWithMessage struct {
|
32
|
- Err error
|
47
|
+ Err error `json:"-"`
|
33
|
ErrorCode
|
48
|
ErrorCode
|
34
|
}
|
49
|
}
|
35
|
|
50
|
|
|
|
51
|
+var (
|
|
|
52
|
+ _ CustomErrParse = new(ErrWithMessage)
|
|
|
53
|
+ _ error = new(ErrWithMessage)
|
|
|
54
|
+)
|
|
|
55
|
+
|
36
|
//NewErrWithMessage 构建错误返回
|
56
|
//NewErrWithMessage 构建错误返回
|
37
|
//code:用于匹配统一消息错误编码 eRR:填充嵌套错误
|
57
|
//code:用于匹配统一消息错误编码 eRR:填充嵌套错误
|
38
|
func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage {
|
58
|
func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage {
|
39
|
r := &ErrWithMessage{
|
59
|
r := &ErrWithMessage{
|
40
|
- ErrorCode: errmessge.Search(code),
|
60
|
+ ErrorCode: SearchErr(code),
|
41
|
}
|
61
|
}
|
42
|
if len(eRR) > 0 {
|
62
|
if len(eRR) > 0 {
|
43
|
r.Err = eRR[0]
|
63
|
r.Err = eRR[0]
|
|
@@ -58,7 +78,7 @@ func (e ErrWithMessage) Unwrap() error { |
|
@@ -58,7 +78,7 @@ func (e ErrWithMessage) Unwrap() error { |
58
|
}
|
78
|
}
|
59
|
|
79
|
|
60
|
//ParseToMessage 实现CustomErrParse的接口
|
80
|
//ParseToMessage 实现CustomErrParse的接口
|
61
|
-func (e ErrWithMessage) ParseToMessage() interface{} {
|
81
|
+func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
|
62
|
return &ResponseMessage{
|
82
|
return &ResponseMessage{
|
63
|
ErrorCode: e.ErrorCode,
|
83
|
ErrorCode: e.ErrorCode,
|
64
|
Data: nil,
|
84
|
Data: nil,
|