This commit is contained in:
2024-08-26 17:20:13 +08:00
commit 51090658a2
39 changed files with 2231 additions and 0 deletions

47
utils/exception/code.go Normal file
View File

@@ -0,0 +1,47 @@
package exception
const (
SUCCESS = 0 // 操作成功
ERROR = 10000 // 默认错误返回
SERVER_ERROR = 10001 // 服务器错误
UNKNOW_ERROR = 10002 // 未知错误
REMOTE_ERROR = 10003 // 远程服务错误
IP_LINMIT = 10004 // 地址限制
IP_LINMIT_CODE = 10014 // 地址限制需要验证码
NO_PERMISSION = 10005 // 未拥有授权
PARAMETER_ERROR = 10008 // 参数错误
RPC_ERROR = 10013 // RPC通讯错误
NOT_FOUND_ROUTE = 10020 // 未查询到路由
NOT_FOUND_METH = 10021 // 未查询到方式
NOT_FOUND = 10022 // 未查询到
AUTH_ERROR = 10023 // 认证错误
NO_VALID_TOKEN = 10024 // token无效
REPEAD = 10025 // 重复数据或操作
OUT_SLINCE = 10026 // 超出限制
DB_ERRROR = 10027 // 数据库错误
SENSITIVE = 10028 // 敏感词语
)
const (
//用户
NOT_FOUND_USER = 20001 // 未查询到用户
EXIST_USER = 20002 // 用户已存在
NO_BELONG_ACTION = 20003 // 越权操作
NO_PHONE = 20004 // 未验证手机号
//订单
ORDER_ERROR = 20100 // 订单统用错误
NO_ORDER = 20101 // 订单不存在
NO_PAID_ORDER = 20102 // 订单未支付
PAID_ORDER = 20103 // 订单已支付
EXPIRE_ORDER = 20104 // 订单超时
DONE_ORDER = 20105 // 订单已完结
NO_BELONG_ORDER = 20106 // 不属于自己的订单
//积分
SIGNED = 20200 // 已签到
NO_ENOUGH_POINT = 20201 // 积分不足
NO_ILLEGAL_CHANNEL = 20202 // 渠道非法
NO_ILLEGAL_SIGN = 20203 // 渠道非签到
)

View File

@@ -0,0 +1,81 @@
package exception
import (
"energy-management-system/utils"
"net/http"
"github.com/gin-gonic/gin"
)
type Exception struct {
HttpCode int `json:"-"`
Code int `json:"code"`
Msg interface{} `json:"msg"`
Err error `json:"err"`
}
type ExceptionResponse struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
Path string `json:"path"`
}
func (r *Exception) Error() interface{} {
return r.Msg
}
func NotFoundR(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_ROUTE, "Not Found Route", utils.GetReqPath(c)})
}
func NotFoundM(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_METH, "Not Found Method", utils.GetReqPath(c)})
}
func Panic(c *gin.Context, e *Exception) {
c.JSON(e.HttpCode, ExceptionResponse{e.Code, e.Msg, utils.GetReqPath(c)})
}
func Unknow(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{UNKNOW_ERROR, "未知错误", utils.GetReqPath(c)})
}
func Server(c *gin.Context) {
c.JSON(http.StatusInternalServerError, ExceptionResponse{SERVER_ERROR, "服务器错误", utils.GetReqPath(c)})
}
// FailMsg 主动抛出错误Exception类型
func FailMsg(msg interface{}) *Exception {
return &Exception{http.StatusOK, ERROR, msg, nil}
}
func FailCodeMsg(code int, msg string) *Exception {
return &Exception{http.StatusOK, code, msg, nil}
}
func PanicMsg(msg interface{}) {
PanicMsgBool(true, msg)
}
func PanicCodeMsg(code int, msg string) {
PanicCodeMsgBool(true, code, msg)
}
func PanicMsgBool(flag bool, msg interface{}) {
if flag {
panic(&Exception{http.StatusOK, ERROR, msg, nil})
}
}
func PanicCodeMsgBool(flag bool, code int, msg string) {
if flag {
panic(&Exception{http.StatusOK, code, msg, nil})
}
}
func PanicMsgErr(err error, msg interface{}) {
if err != nil {
panic(&Exception{http.StatusOK, ERROR, msg, err})
}
}
func PanicCodeMsgErr(err error, code int, msg string) {
if err != nil {
panic(&Exception{http.StatusOK, code, msg, err})
}
}