Creating recoverPanic method inside our middleware.go file and use it inside routes.go wrapping our router.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 59s

This commit is contained in:
Maxime Delporte
2025-10-23 14:19:53 +02:00
parent 2124f2f882
commit 7057f89038
2 changed files with 26 additions and 1 deletions

24
cmd/api/middleware.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"fmt"
"net/http"
)
func (app *application) recoverPanic(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Create a deferred function (which will always be run in the event of a panic as Go unwinds the stack).
defer func() {
// Use the builtin recover function to check if there has been a panic or not.
if err := recover(); err != nil {
// If there was a panic, set a "Connection: close" header on the response. This acts as a trigger to make Go's HTTP server automatically close the current connection after a response has been sent.
w.Header().Set("Connection", "close")
// The value returned by recover() has the type any, so we use fmt.Errorf() to normalize it into an error and call our serverErrorResponse() helper. In turn, this will log the error using our custom Logger type at the ERROR level and send the client a 500 Internal Server Error response.
app.serverErrorResponse(w, r, fmt.Errorf("%s", err))
}
}()
next.ServeHTTP(w, r)
})
}

View File

@@ -26,5 +26,6 @@ func (app *application) routes() http.Handler {
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler) router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
// Return the httprouter instance. // Return the httprouter instance.
return router // Wrap the router with the panic recovery middleware
return app.recoverPanic(router)
} }