2021-05-02 22:49:16 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2021-07-18 14:12:57 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func recoveryMiddleware() func(next http.Handler) http.Handler {
|
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{})
|
2021-10-11 15:51:49 +00:00
|
|
|
l := log.WithField("logger", "authentik.router.sentry")
|
2021-05-02 22:49:16 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
sentryHandler.Handle(next)
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
defer func() {
|
|
|
|
re := recover()
|
|
|
|
if re == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := re.(error)
|
|
|
|
if err != nil {
|
2021-07-18 14:12:57 +00:00
|
|
|
l.WithError(err).Warning("global panic handler")
|
2021-05-02 22:49:16 +00:00
|
|
|
jsonBody, _ := json.Marshal(struct {
|
|
|
|
Successful bool
|
|
|
|
Error string
|
|
|
|
}{
|
|
|
|
Successful: false,
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2021-07-18 14:12:57 +00:00
|
|
|
_, err := w.Write(jsonBody)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Warning("Failed to write sentry error body")
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|