This commit is contained in:
lbenedar
2026-03-10 16:00:53 +03:00
parent b4ea20d1d3
commit 71f15d1131
4 changed files with 43 additions and 3 deletions

37
cmd/api/errors.go Normal file
View File

@@ -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)
}

View File

@@ -15,6 +15,6 @@ 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.Print(err) 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)
} }
} }

View File

@@ -15,7 +15,7 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) { func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r) id, err := app.readIDParam(r)
if err != nil { if err != nil {
http.NotFound(w, r) app.notFoundResponse(w, r)
return 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) err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil { if err != nil {
app.logger.Print(err) 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)
} }
} }

View File

@@ -9,6 +9,9 @@ import (
func (app *application) routes() *httprouter.Router { func (app *application) routes() *httprouter.Router {
router := httprouter.New() 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.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler) router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler) router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)