lets-go:6.4 panic recovery
This commit is contained in:
parent
bee34e7405
commit
2b57596bbc
@ -15,7 +15,6 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
snippets, err := app.snippets.Latest()
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -19,6 +20,20 @@ func (app *application) logRequest(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// recoverPanic ...
|
||||
func (app *application) recoverPanic(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
w.Header().Set("Connection", "close")
|
||||
app.serverError(w, r, fmt.Errorf("%s", err))
|
||||
}
|
||||
}()
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func secureHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
|
@ -16,5 +16,5 @@ func (app *application) routes() http.Handler {
|
||||
mux.HandleFunc("/snippet/view", app.snippetView)
|
||||
mux.HandleFunc("/snippet/create", app.snippetCreate)
|
||||
|
||||
return app.logRequest(secureHeaders(mux))
|
||||
return app.recoverPanic(app.logRequest(secureHeaders(mux)))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user