-
Notifications
You must be signed in to change notification settings - Fork 0
/
scout.go
36 lines (33 loc) · 1.01 KB
/
scout.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
34
35
36
// Package scout implement a middleware for report panic to sentry.io.
package scout
import (
"fmt"
"net/http"
"os"
"runtime/debug"
"github.com/getsentry/sentry-go"
"github.com/go-chi/chi/middleware"
)
// SentryRecovery recover from panic, report the error back to sentry, log it,
// and return an internal server error back to the user.
func SentryRecovery(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
logEntry := middleware.GetLogEntry(r)
if logEntry != nil {
logEntry.Panic(rval, debug.Stack())
} else {
fmt.Fprintf(os.Stderr, "Panic: %+v\n", rval)
debug.PrintStack()
}
rvalStr := fmt.Sprint(rval)
var event = sentry.NewEvent()
event.Message = rvalStr
sentry.CaptureEvent(event)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
handler.ServeHTTP(w, r)
})
}