Add recovery to http handler

This commit is contained in:
Christian Claus 2018-04-23 21:27:36 +02:00
parent f495623b2a
commit 52ea329b8b

View file

@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/net/webdav"
"net/http"
"errors"
)
func main() {
@ -31,7 +32,7 @@ func main() {
Handler: wdHandler,
}
http.Handle("/", app.NewBasicAuthWebdavHandler(a))
http.Handle("/", wrapRecovery(app.NewBasicAuthWebdavHandler(a)))
connAddr := fmt.Sprintf("%s:%s", config.Address, config.Port)
@ -51,3 +52,25 @@ func main() {
log.Fatal(http.ListenAndServe(connAddr, nil))
}
}
func wrapRecovery(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
defer func() {
r := recover()
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = errors.New("Unknown error")
}
log.WithError(err).Error("An error occurred handling a webdav request")
http.Error(w, "Internal server error", http.StatusInternalServerError)
}()
handler.ServeHTTP(w, r)
})
}