blob: 1537f55e14ff7573f10b41ce2ea225d79daf3faa (
plain) (
blame)
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
|
package kcore
import (
"errors"
"net/http"
"golang.org/x/exp/slog"
)
func RecoverMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rcv := recover(); rcv != nil {
switch err := rcv.(type) {
case error:
if errors.Is(err, http.ErrAbortHandler) {
panic(rcv)
}
slog.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
default:
panic(err)
}
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
|