probo/cmd/serve/recover.go

22 lines
405 B
Go

package serve
import (
"net/http"
"github.com/charmbracelet/log"
)
func Recover(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err != nil {
log.Error("Recovering from", "err", err)
http.Error(w, err.(error).Error(), http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}