diff --git a/cmd/api/errors.go b/cmd/api/errors.go index 8bd3c52..2c12cc8 100644 --- a/cmd/api/errors.go +++ b/cmd/api/errors.go @@ -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()) +} diff --git a/cmd/api/movies.go b/cmd/api/movies.go index 158afad..ed11db6 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -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 }