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

48
middleware/recovery.go Normal file
View File

@@ -0,0 +1,48 @@
package middleware
import (
"energy-management-system/utils"
"energy-management-system/utils/exception"
"fmt"
"github.com/gin-gonic/gin"
"net"
"os"
"strings"
"time"
)
func Recovery() 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()
}
}