Using the new error's helper methods inside our current API endpoint and with our httprouter Handlers NotFound and MethodNotAllowed (will override the default responses with our helper's methods)
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 52s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 52s
This commit is contained in:
@@ -21,7 +21,7 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques
|
|||||||
|
|
||||||
err := app.writeJSON(w, http.StatusOK, env, nil)
|
err := app.writeJSON(w, http.StatusOK, env, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Error(err.Error())
|
// Use the new serverErrorResponse() helper
|
||||||
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
app.serverErrorResponse(w, r, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
|
|||||||
id, err := app.readIDParam(r)
|
id, err := app.readIDParam(r)
|
||||||
|
|
||||||
if err != nil || id < 1 {
|
if err != nil || id < 1 {
|
||||||
http.NotFound(w, r)
|
// Use the new notFoundResponse() helper
|
||||||
|
app.notFoundResponse(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
|
|||||||
// Encode the struct to JSON and send it as the HTTP response.
|
// Encode the struct to JSON and send it as the HTTP response.
|
||||||
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
|
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Error(err.Error())
|
// Use the new serverErrorResponse() helper
|
||||||
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
app.serverErrorResponse(w, r, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ func (app *application) routes() http.Handler {
|
|||||||
// Initialize a new httprouter router instance
|
// Initialize a new httprouter router instance
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
|
|
||||||
|
// Convert the notFoundResponse() helper to a http.Handler using the http.HandlerFunc() adapter, and then set it as the custom error handler for 404 Not Found responses.
|
||||||
|
router.NotFound = http.HandlerFunc(app.notFoundResponse)
|
||||||
|
|
||||||
|
// Likewise, convert the methodNotAllowedResponse() helper to a http.Handler and set it as the custom error handler for 405 Method Not Allowed responses
|
||||||
|
router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Register the relevant methods, URL patterns and handler functions for our
|
Register the relevant methods, URL patterns and handler functions for our
|
||||||
endpoints using the HandlerFunc() method. Note that http.MethodGet and
|
endpoints using the HandlerFunc() method. Note that http.MethodGet and
|
||||||
|
|||||||
Reference in New Issue
Block a user