-
Notifications
You must be signed in to change notification settings - Fork 39
/
recover.go
33 lines (29 loc) · 819 Bytes
/
recover.go
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 mwhttp
import (
"fmt"
"net/http"
"runtime/debug"
"github.com/utrack/clay/v2/server/middlewares/mwcommon"
"github.com/utrack/clay/v2/transport/httpruntime"
"github.com/pkg/errors"
)
// Recover recovers HTTP server from handlers' panics.
func Recover(logger interface{}) Middleware {
logFunc := mwcommon.GetLogFunc(logger)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
stack := string(debug.Stack())
httpruntime.SetError(
r.Context(),
r, w,
errors.Errorf("recovered from panic: %v\nstack:%v", rec, stack),
)
logFunc(r.Context(), fmt.Sprintf("recovered from panic: %v, %v ", rec, stack))
}
}()
next.ServeHTTP(w, r)
})
}
}