增加定时任务 处理异常

This commit is contained in:
2024-08-30 11:55:19 +08:00
parent aa3f9e8711
commit bfb284c4cc
30 changed files with 945 additions and 336 deletions

View File

@@ -1,47 +0,0 @@
package exception
const (
SUCCESS = 1 // 操作成功
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

@@ -1,81 +1,164 @@
package exception
import (
"energy-management-system/utils"
"net/http"
"energy-management-system/utils/code"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
)
type Exception struct {
HttpCode int `json:"-"`
Code int `json:"code"`
Msg interface{} `json:"msg"`
Err error `json:"err"`
type E struct {
Code int `json:"code"`
Msg any `json:"msg"`
Err error `json:"-"`
}
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 (r E) Error() string {
if r.Err != nil {
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\",\"err\":\"%s\"}`, r.Code, r.Msg, r.Err.Error())
} else {
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\"}`, r.Code, r.Msg)
}
}
func PanicMsgErr(err error, msg interface{}) {
func (r E) Return(c *gin.Context) {
c.JSON(http.StatusOK, r)
}
func NE(msg any) error {
return NEC(msg, code.ERROR)
}
func NEC(msg any, code int) error {
return &E{
Msg: msg,
Code: code,
}
}
func NEE(err error, msg string) error {
return NEEC(err, msg, code.ERROR)
}
func NEEC(err error, msg string, code int) error {
if err != nil {
panic(&Exception{http.StatusOK, ERROR, msg, err})
return &E{
Msg: msg,
Code: code,
Err: err,
}
}
return nil
}
func PM(msg any) {
panic(&E{code.ERROR, msg, nil})
}
func PMC(msg any, code int) {
panic(&E{code, msg, nil})
}
func PBM(flag bool, msg any) {
if flag {
panic(&E{code.ERROR, msg, nil})
}
}
func PanicCodeMsgErr(err error, code int, msg string) {
func PBMC(flag bool, msg any, code int) {
if flag {
panic(&E{code, msg, nil})
}
}
func PEM(err error, msg any) {
if err != nil {
panic(&Exception{http.StatusOK, code, msg, err})
panic(&E{code.ERROR, msg, err})
}
}
func PEMC(err error, msg any, code int) {
if err != nil {
panic(&E{code, msg, err})
}
}
func PDM(db *gorm.DB, msgs ...string) {
if db.Error != nil {
msg := "未查询到"
if len(msgs) > 0 {
msg = msgs[0]
}
panic(&E{code.ERROR, msg, db.Error})
}
}
//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})
// }
//}