mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
45 lines
999 B
Go
45 lines
999 B
Go
package middleware
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"fotbal-club/internal/services"
|
|
"fotbal-club/pkg/logger"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ErrorStatusReporter(reporter *services.ErrorReporter) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
if reporter == nil {
|
|
return
|
|
}
|
|
status := c.Writer.Status()
|
|
if status >= 500 {
|
|
msg := ""
|
|
if len(c.Errors) > 0 {
|
|
var parts []string
|
|
for _, e := range c.Errors {
|
|
if e != nil && e.Err != nil {
|
|
parts = append(parts, e.Err.Error())
|
|
} else if e != nil {
|
|
parts = append(parts, e.Error())
|
|
}
|
|
}
|
|
msg = strings.Join(parts, "; ")
|
|
}
|
|
reporter.Report(c.Request.Context(), &services.ErrorEvent{
|
|
Origin: "backend",
|
|
Language: "go",
|
|
Severity: "error",
|
|
Message: msg,
|
|
URL: c.Request.URL.Path,
|
|
Method: c.Request.Method,
|
|
Status: status,
|
|
RequestID: GetRequestID(c),
|
|
})
|
|
logger.Error("Reported 5xx status=%d path=%s", status, c.Request.URL.Path)
|
|
}
|
|
}
|
|
}
|