增加定时任务 处理异常
This commit is contained in:
36
utils/recovery/cron_recovery.go
Normal file
36
utils/recovery/cron_recovery.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package recovery
|
||||
|
||||
import (
|
||||
"energy-management-system/core/logger"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/utils/code"
|
||||
"energy-management-system/utils/exception"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func CronRecovery(name string) {
|
||||
if err := recover(); err != nil {
|
||||
var codes int
|
||||
var msg any
|
||||
var er error
|
||||
if h, ok := err.(*exception.E); ok {
|
||||
codes = h.Code
|
||||
msg = h.Msg
|
||||
er = h.Err
|
||||
} else if e, ok := err.(error); ok {
|
||||
msg = fmt.Sprint("未知错误:", e.Error())
|
||||
logger.StackSend(5, e.Error())
|
||||
codes = code.UNKNOW_ERROR
|
||||
} else {
|
||||
msg = fmt.Sprint("服务器错误:", err)
|
||||
logger.StackSend(5, err.(string))
|
||||
codes = code.SERVER_ERROR
|
||||
}
|
||||
global.Log.WithFields(map[string]any{
|
||||
"code": codes,
|
||||
"model": "task",
|
||||
"func": name,
|
||||
"err": er,
|
||||
}).Error(msg)
|
||||
}
|
||||
}
|
||||
86
utils/recovery/gin_recovery.go
Normal file
86
utils/recovery/gin_recovery.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package recovery
|
||||
|
||||
import (
|
||||
"energy-management-system/core/logger"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/utils"
|
||||
"energy-management-system/utils/code"
|
||||
"energy-management-system/utils/exception"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GinRecovery(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
var brokenPipe bool
|
||||
if ne, ok := err.(*net.OpError); ok {
|
||||
if se, ok := ne.Err.(*os.SyscallError); ok {
|
||||
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
||||
brokenPipe = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if brokenPipe {
|
||||
c.Error(err.(error))
|
||||
} else {
|
||||
if req, ok := c.Get("_req"); ok {
|
||||
global.Log.Errorln("获取参数:", utils.String(req))
|
||||
}
|
||||
if h, ok := err.(*exception.E); ok {
|
||||
h.Return(c)
|
||||
c.Errors = append(c.Errors, &gin.Error{Meta: h})
|
||||
} else if e, ok := err.(error); ok {
|
||||
global.Log.Errorln("未知错误:", err)
|
||||
logger.StackSend(3, e.Error())
|
||||
c.JSON(http.StatusForbidden, exception.E{code.UNKNOW_ERROR, "未知错误", nil})
|
||||
} else {
|
||||
global.Log.Errorln("服务器错误:", err)
|
||||
logger.StackSend(3, err.(string))
|
||||
c.JSON(http.StatusForbidden, exception.E{code.SERVER_ERROR, "服务器错误", nil})
|
||||
}
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
|
||||
//func GinRecovery() gin.HandlerFunc {
|
||||
// return func(c *gin.Context) {
|
||||
// defer func() {
|
||||
// if err := recover(); err != nil {
|
||||
// var brokenPipe bool
|
||||
// if ne, ok := err.(*net.OpError); ok {
|
||||
// if se, ok := ne.Err.(*os.SyscallError); ok {
|
||||
// if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
||||
// brokenPipe = true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if brokenPipe {
|
||||
// c.Error(err.(error))
|
||||
// } else {
|
||||
// if h, ok := err.(*exception.Exception); ok {
|
||||
// exception.Panic(c, h)
|
||||
// c.Errors = append(c.Errors, &gin.Error{Meta: h})
|
||||
// } else if _, ok = err.(error); ok {
|
||||
// if gin.IsDebugging() {
|
||||
// fmt.Printf("[Recovery] %s : %s", utils.TimeFormat(time.Now()), err)
|
||||
// utils.Stack(3)
|
||||
// }
|
||||
// exception.Unknow(c)
|
||||
// } else {
|
||||
// fmt.Print(err)
|
||||
// exception.Server(c)
|
||||
// }
|
||||
// }
|
||||
// c.Abort()
|
||||
// }
|
||||
// }()
|
||||
// c.Next()
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user