mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Testing for controller/main.go
This commit is contained in:
57
pkg/serializer/common.go
Normal file
57
pkg/serializer/common.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package serializer
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// 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
|
||||
// CodeDBError 数据库操作失败
|
||||
CodeDBError = 50001
|
||||
// CodeEncryptError 加密失败
|
||||
CodeEncryptError = 50002
|
||||
//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
|
||||
}
|
||||
Reference in New Issue
Block a user