diff --git a/cmd/api/errors.go b/cmd/api/errors.go new file mode 100644 index 0000000..79c7a99 --- /dev/null +++ b/cmd/api/errors.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "net/http" +) + +func (app *application) logError(r *http.Request, err error) { + app.logger.Print(err) +} + +func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) { + env := envelope{"error": message} + + err := app.writeJSON(w, status, env, nil) + if err != nil { + app.logError(r, err) + w.WriteHeader(500) + } +} + +func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Request, err error) { + app.logError(r, err) + + message := "the server encountered a problem and could not process your request" + app.errorResponse(w, r, http.StatusInternalServerError, message) +} + +func (app *application) notFoundResponse(w http.ResponseWriter, r *http.Request) { + message := "the requested resource could not be found" + app.errorResponse(w, r, http.StatusNotFound, message) +} + +func (app *application) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request) { + message := fmt.Sprintf("the %s method is not supported for this resource", r.Method) + app.errorResponse(w, r, http.StatusMethodNotAllowed, message) +} diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go index 0f0a316..052cf41 100644 --- a/cmd/api/healthcheck.go +++ b/cmd/api/healthcheck.go @@ -15,6 +15,6 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques err := app.writeJSON(w, http.StatusOK, env, nil) if err != nil { app.logger.Print(err) - http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) + app.serverErrorResponse(w, r, err) } } diff --git a/cmd/api/movies.go b/cmd/api/movies.go index 585e96d..bdf11be 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -15,7 +15,7 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) { id, err := app.readIDParam(r) if err != nil { - http.NotFound(w, r) + app.notFoundResponse(w, r) return } @@ -31,6 +31,6 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) if err != nil { app.logger.Print(err) - http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) + app.serverErrorResponse(w, r, err) } } diff --git a/cmd/api/routes.go b/cmd/api/routes.go index 0dc2429..d09bd9b 100644 --- a/cmd/api/routes.go +++ b/cmd/api/routes.go @@ -9,6 +9,9 @@ import ( func (app *application) routes() *httprouter.Router { router := httprouter.New() + router.NotFound = http.HandlerFunc(app.notFoundResponse) + router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse) + router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler) router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler) router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)