1 Star 0 Fork 0

LonelyPale / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
message.go 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
LonelyPale 提交于 2021-02-13 17:49 . update message
package goutils
import (
"github.com/LonelyPale/goutils/errors/ecode"
"github.com/LonelyPale/goutils/validator"
)
type Message struct {
Code int `json:"code"` //状态码
Msg string `json:"msg,omitempty"` //消息
Data interface{} `json:"data,omitempty"` //结果数据
}
func NewMessage(code int, msg string, datas ...interface{}) *Message {
var data interface{}
if len(datas) == 1 {
data = datas[0]
} else if len(datas) > 1 {
data = datas
}
return &Message{Code: code, Msg: msg, Data: data}
}
func NewSuccessMessage(datas ...interface{}) *Message {
return NewMessage(ecode.StatusOK, ecode.StatusText(ecode.StatusOK), datas...)
}
func NewErrorMessage(err error) *Message {
switch e := err.(type) {
case ecode.ErrorCode:
return &Message{Code: e.Code(), Msg: e.Error(), Data: e.Details()}
case validator.ValidationErrors:
return &Message{Code: ecode.StatusError, Msg: e.Error(), Data: e}
default:
emsg := e.Error()
if len(emsg) > 0 {
return &Message{Code: ecode.StatusError, Msg: emsg}
} else {
return &Message{Code: ecode.StatusError, Msg: ecode.StatusText(ecode.StatusError)}
}
}
}
Go
1
https://gitee.com/lonelypalegit/goutils.git
git@gitee.com:lonelypalegit/goutils.git
lonelypalegit
goutils
goutils
master

搜索帮助