...
|
...
|
@@ -4,11 +4,26 @@ import ( |
|
|
"encoding/json"
|
|
|
)
|
|
|
|
|
|
//CustomErrParse 解析自定义错误结构体
|
|
|
type CustomErrParse interface {
|
|
|
ParseToMessage() interface{}
|
|
|
ParseToMessage() *ResponseMessage
|
|
|
}
|
|
|
|
|
|
//ErrorCode 统一错误编码结构
|
|
|
//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{}
|
|
|
}
|
|
|
|
|
|
//ErrorCode 统一错误结构
|
|
|
type ErrorCode struct {
|
|
|
Errno int `json:"code"`
|
|
|
Errmsg string `json:"msg"`
|
...
|
...
|
@@ -22,22 +37,27 @@ type ResponseMessage struct { |
|
|
|
|
|
func NewMesage(code int) *ResponseMessage {
|
|
|
return &ResponseMessage{
|
|
|
ErrorCode: errmessge.Search(code),
|
|
|
ErrorCode: SearchErr(code),
|
|
|
Data: nil,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//ErrWithMessage 自定义错误结构
|
|
|
type ErrWithMessage struct {
|
|
|
Err error
|
|
|
Err error `json:"-"`
|
|
|
ErrorCode
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
_ CustomErrParse = new(ErrWithMessage)
|
|
|
_ error = new(ErrWithMessage)
|
|
|
)
|
|
|
|
|
|
//NewErrWithMessage 构建错误返回
|
|
|
//code:用于匹配统一消息错误编码 eRR:填充嵌套错误
|
|
|
func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage {
|
|
|
r := &ErrWithMessage{
|
|
|
ErrorCode: errmessge.Search(code),
|
|
|
ErrorCode: SearchErr(code),
|
|
|
}
|
|
|
if len(eRR) > 0 {
|
|
|
r.Err = eRR[0]
|
...
|
...
|
@@ -58,7 +78,7 @@ func (e ErrWithMessage) Unwrap() error { |
|
|
}
|
|
|
|
|
|
//ParseToMessage 实现CustomErrParse的接口
|
|
|
func (e ErrWithMessage) ParseToMessage() interface{} {
|
|
|
func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
|
|
|
return &ResponseMessage{
|
|
|
ErrorCode: e.ErrorCode,
|
|
|
Data: nil,
|
...
|
...
|
|