diff options
author | Alexander <a.kiryukhin@vk.team> | 2024-06-11 14:12:30 +0300 |
---|---|---|
committer | Alexander <a.kiryukhin@vk.team> | 2024-06-11 14:12:30 +0300 |
commit | 88a6b1cb628e4e334068e861ac5fb56274d88845 (patch) | |
tree | 312f2a3a7182b6e2fc45e87ec71ddde9861c77f8 /middleware/recover.go |
Diffstat (limited to 'middleware/recover.go')
-rw-r--r-- | middleware/recover.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/middleware/recover.go b/middleware/recover.go new file mode 100644 index 0000000..6b5f2cb --- /dev/null +++ b/middleware/recover.go @@ -0,0 +1,34 @@ +package middleware + +import ( + "net/http" + "runtime/debug" + + "log/slog" +) + +func Recover(logger *slog.Logger) 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 + } + debug.PrintStack() + 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) + }) + } +} |