finger/internal/middleware/recover.go
Gustavo Maronato f96dda4af2
Some checks failed
Go / checks (push) Failing after 52s
refactor and add tests
2023-09-20 01:26:38 -03:00

28 lines
506 B
Go

package middleware
import (
"log/slog"
"net/http"
"git.maronato.dev/maronato/finger/internal/log"
)
func Recoverer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
l := log.FromContext(ctx)
defer func() {
err := recover()
if err != nil {
l.Error("Panic", slog.Any("error", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}()
next.ServeHTTP(w, r)
})
}