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
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 53s
This commit is contained in:
@@ -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.
|
// 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)
|
err := app.writeJSON(w, status, env, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logError(r, err)
|
app.logError(r, err)
|
||||||
w.WriteHeader(500)
|
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)
|
message := fmt.Sprintf("the %s method is not supported for this resource", r.Method)
|
||||||
app.errorResponse(w, r, http.StatusMethodNotAllowed, message)
|
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())
|
||||||
|
}
|
||||||
|
|||||||
@@ -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.
|
// 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)
|
err := app.readJSON(w, r, &input)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
app.badRequestResponse(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user