mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-05 03:02:56 +00:00
dev day #89
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"runtime/debug"
|
||||
|
||||
"fotbal-club/pkg/logger"
|
||||
"fotbal-club/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -41,3 +42,40 @@ func CustomRecovery() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func CustomRecoveryWithReporter(reporter *services.ErrorReporter) gin.HandlerFunc {
|
||||
if reporter == nil {
|
||||
return CustomRecovery()
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
stack := string(debug.Stack())
|
||||
requestID := GetRequestID(c)
|
||||
logger.Error("Panic recovered",
|
||||
"request_id", requestID,
|
||||
"error", fmt.Sprintf("%v", err),
|
||||
"stack", stack,
|
||||
"path", c.Request.URL.Path,
|
||||
"method", c.Request.Method,
|
||||
)
|
||||
reporter.Report(c.Request.Context(), &services.ErrorEvent{
|
||||
Origin: "backend",
|
||||
Language: "go",
|
||||
Severity: "fatal",
|
||||
Message: fmt.Sprintf("%v", err),
|
||||
Stack: stack,
|
||||
URL: c.Request.URL.Path,
|
||||
Method: c.Request.Method,
|
||||
RequestID: requestID,
|
||||
})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Internal server error",
|
||||
"request_id": requestID,
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user