protocol.go
2.7 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package protocol
import (
"encoding/json"
"github.com/linmadan/egglib-go/core/application"
)
//CustomErrParse 解析自定义错误结构体
type CustomErrParse interface {
ParseToMessage() *ResponseMessage
}
//ErrorMap 统一消息错误编码
type ErrorMap map[int]string
//Search 搜索错误描述
func (m ErrorMap) Search(code int) ErrorCode {
if v, ok := m[code]; ok {
return ErrorCode{
Errno: code,
Errmsg: v,
}
}
return ErrorCode{Errno: code, Errmsg: "错误码未定义"}
}
func NewMesage(code int) *ResponseMessage {
return &ResponseMessage{
ErrorCode: SearchErr(code),
Data: struct {
}{},
}
}
var (
_ CustomErrParse = new(ErrWithMessage)
_ error = new(ErrWithMessage)
)
//Error 实现接口error 中的方法
//将ErrorCode转为json数据,建议用于日志记录
func (e ErrWithMessage) Error() string {
bt, _ := json.Marshal(e.ErrorCode)
return string(bt)
}
//Unwrap 接口实现
func (e ErrWithMessage) Unwrap() error {
return e.Err
}
//ParseToMessage 实现CustomErrParse的接口
func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
return &ResponseMessage{
ErrorCode: e.ErrorCode,
Data: nil,
}
}
func SearchErr(code int) ErrorCode {
return errmessge.Search(code)
}
func NewResponseMessageData(data interface{}, err error) *ResponseMessage {
var msg *ResponseMessage
if err == nil {
msg = NewMesage(0)
msg.Data = data
return msg
}
//log.Error("服务错误:" + eRR.Error())
if x, ok := err.(CustomErrParse); ok {
msg = x.ParseToMessage()
msg.Data = data
return msg
}
if v, ok := err.(*application.ServiceError); ok {
msg = &ResponseMessage{
ErrorCode{v.Code, v.Message},
data,
}
return msg
}
return NewMesage(1)
}
func NewResponseMessageListData(data interface{}, err error) *ResponseMessage {
mapData := map[string]interface{}{"gridResult": data}
return NewResponseMessageData(mapData, err)
}
func NewResponseMessage(code int, err string) *ResponseMessage {
return &ResponseMessage{
ErrorCode: ErrorCode{
Errno: code,
Errmsg: err,
},
Data: struct {
}{},
}
}
func NewCustomMessage(code int, msg string) *ErrWithMessage {
return &ErrWithMessage{
Err: nil,
ErrorCode: ErrorCode{code, msg},
}
}
//ErrorCode 统一错误结构
type ErrorCode struct {
Errno int `json:"code"`
Errmsg string `json:"msg"`
}
//ResponseMessage 统一返回消息结构体
type ResponseMessage struct {
ErrorCode
Data interface{} `json:"data"`
}
//ErrWithMessage 自定义错误结构
type ErrWithMessage struct {
Err error `json:"-"`
ErrorCode
}
var errmessge ErrorMap = map[int]string{
0: "成功",
1: "系统异常",
2: "参数错误",
}
type RequestHeader struct {
UserId int64 //UserId 唯一标识
}