add NotFound and MethodNotAllowed routes

This commit is contained in:
lyx0 2022-08-17 23:14:05 +02:00
parent a72bb0b066
commit 661a30542b
2 changed files with 9 additions and 3 deletions

View file

@ -12,9 +12,7 @@ func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Reque
app.errorResponse(w, r, http.StatusInternalServerError, message)
}
// The logError() method is a generic helper for logging an error message. Later in the
// book we'll upgrade this to use structured logging, and record additional information
// about the request including the HTTP method and URL.
// The logError() method is a generic helper for logging an error message.
func (app *application) logError(r *http.Request, err error) {
app.Logger.Errorw("Error",
"Request URI", r.RequestURI,
@ -43,3 +41,8 @@ func (app *application) editConflictResponse(w http.ResponseWriter, r *http.Requ
message := "unable to update the record due to an edit conflict, please try again"
app.errorResponse(w, r, http.StatusConflict, 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

@ -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/commands/:name", app.showCommandHandler)
router.HandlerFunc(http.MethodPost, "/v1/commands", app.createCommandHandler)
router.HandlerFunc(http.MethodPatch, "/v1/commands/:name", app.updateCommandHandler)