Creating badRequestResponse helper method inside errors.go and use in in the createMovieHandler.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 53s

This commit is contained in:
Maxime Delporte
2025-10-24 18:55:05 +02:00
parent 12e1c7a523
commit 906bd868f9
2 changed files with 7 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, st
// Write the response using the writeJSON() helper. If this happens to return an error then og it, and fall back to sending the client an empty response with a 500 Internal Server Error status code.
err := app.writeJSON(w, status, env, nil)
if err != nil {
app.logError(r, err)
w.WriteHeader(500)
@@ -46,3 +47,7 @@ func (app *application) methodNotAllowedResponse(w http.ResponseWriter, r *http.
message := fmt.Sprintf("the %s method is not supported for this resource", r.Method)
app.errorResponse(w, r, http.StatusMethodNotAllowed, message)
}
func (app *application) badRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
}

View File

@@ -19,8 +19,9 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
// Use the new readJSON() helper to decode the request body into the input struct. If this returns an error, we send the client the error message along with a 400 Bad Request status code, just like before.
err := app.readJSON(w, r, &input)
if err != nil {
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
app.badRequestResponse(w, r, err)
return
}