增加定时任务 处理异常

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,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})
// }
//}