Files
MyClub/internal/middleware/error_status_reporter.go
T
Tomas Dvorak 8762bde4bf dev day #89
2025-11-11 10:29:30 +01:00

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