You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/serializer/common.go

64 lines
1.6 KiB

5 years ago
package serializer
import "github.com/gin-gonic/gin"
5 years ago
// Response 基础序列化器
type Response struct {
Code int `json:"code"`
Data interface{} `json:"data,omitempty"`
Msg string `json:"msg"`
Error string `json:"error,omitempty"`
}
// 三位数错误编码为复用http原本含义
// 五位数错误编码为应用自定义错误
// 五开头的五位数错误编码为服务器端错误,比如数据库操作失败
// 四开头的五位数错误编码为客户端错误,有时候是客户端代码写错了,有时候是用户操作错误
const (
// CodeCheckLogin 未登录
CodeCheckLogin = 401
// CodeNoRightErr 未授权访问
CodeNoRightErr = 403
// CodeUploadFailed 上传出错
CodeUploadFailed = 4001
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败
CodeEncryptError = 50002
// CodePolicyNotAllowed 当前存储策略不允许
CodePolicyNotAllowed = 50003
// CodeIOFailed IO操作失败
CodeIOFailed = 50004
//CodeParamErr 各种奇奇怪怪的参数错误
CodeParamErr = 40001
)
// DBErr 数据库操作失败
func DBErr(msg string, err error) Response {
if msg == "" {
msg = "数据库操作失败"
}
return Err(CodeDBError, msg, err)
}
// ParamErr 各种参数错误
func ParamErr(msg string, err error) Response {
if msg == "" {
msg = "参数错误"
}
return Err(CodeParamErr, msg, err)
}
// Err 通用错误处理
func Err(errCode int, msg string, err error) Response {
res := Response{
Code: errCode,
Msg: msg,
}
// 生产环境隐藏底层报错
if err != nil && gin.Mode() != gin.ReleaseMode {
res.Error = err.Error()
}
return res
}