summaryrefslogtreecommitdiff
path: root/middleware/recover.go
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:38:17 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:38:17 +0300
commit2916082d5ed94ef86ad58bdb7256ae07b214c4f3 (patch)
tree322a0e9172c07457a892f9737839843b8c584864 /middleware/recover.go
Начальный коммит
Diffstat (limited to 'middleware/recover.go')
-rw-r--r--middleware/recover.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/middleware/recover.go b/middleware/recover.go
new file mode 100644
index 0000000..b34d582
--- /dev/null
+++ b/middleware/recover.go
@@ -0,0 +1,33 @@
+package middleware
+
+import (
+ "log/slog"
+ "net/http"
+
+ "go.neonxp.ru/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)
+ })
+ }
+}