1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package middleware
import (
"log/slog"
"net/http"
"neonxp.ru/go/mux"
)
func Recover(logger *slog.Logger) mux.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err == nil {
return
}
requestID := GetRequestID(r)
logger.ErrorContext(
r.Context(),
"panic",
slog.Any("panic", err),
slog.String("proto", r.Proto),
slog.String("method", r.Method),
slog.String("request_uri", r.RequestURI),
slog.String("request_id", requestID),
)
}()
next.ServeHTTP(w, r)
})
}
}
|