diff options
author | Matthew Lemon <y@yulqen.org> | 2024-03-13 16:42:27 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-03-13 16:42:27 +0000 |
commit | 033a6e69a473e44ccb7cd66295532846768af114 (patch) | |
tree | 5b72fad2e04f210bdf005e0c6014aae463384f3c | |
parent | 5607c7558178c8cc84ee1ec3d7f4b39dbfc10f4f (diff) |
Implements our own errors
-rw-r--r-- | cmd/api/datamaps.go | 9 | ||||
-rw-r--r-- | cmd/api/errors.go | 45 | ||||
-rw-r--r-- | cmd/api/healthcheck.go | 4 |
3 files changed, 46 insertions, 12 deletions
diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go index c58d811..10dfa38 100644 --- a/cmd/api/datamaps.go +++ b/cmd/api/datamaps.go @@ -29,8 +29,7 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ // Parse the multipart form err := r.ParseMultipartForm(10 << 20) // 10Mb max if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return + app.serverErrorResponse(w, r, err) } // Get form values @@ -77,8 +76,7 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) if err != nil { app.logger.Debug("writing out csv", "err", err) - http.Error(w, "Cannot write output from parsed CSV", http.StatusInternalServerError) - return + app.serverErrorResponse(w, r, err) } // fmt.Fprintf(w, "file successfully uploaded") @@ -89,8 +87,7 @@ func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Reques app.logger.Info("the id requested", "id", id) id_int, err := strconv.ParseInt(id, 10, 64) if err != nil || id_int < 1 { - http.NotFound(w, r) - return + app.notFoundResponse(w, r) } fmt.Fprintf(w, "show the details for datamap %d\n", id_int) } diff --git a/cmd/api/errors.go b/cmd/api/errors.go index 77833f3..2a40bad 100644 --- a/cmd/api/errors.go +++ b/cmd/api/errors.go @@ -1,7 +1,46 @@ package main -import "net/http" +import ( + "fmt" + "net/http" +) -func (app *application) logError(r http.Request, err error) { - app.logger.Info(err) +// The logError() method is a generic helper for logging an error message. +func (app *application) logError(r *http.Request, err error) { + app.logger.Info("dbasik error", "error", err) +} + +// The errorResponse() method is a generic helper for sending JSON-formatted error +// messages to the client with a given status code. Because we are using any we +// we have flexibility over the values that we can include in the response. +func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) { + env := envelope{"error": message} + + // Write the response using the writeJSON() helper. If it returns + // an error, log it and send the client an empty response with a + // 500 Internal Server status code. + if err := app.writeJSON(w, status, env, nil); err != nil { + app.logError(r, err) + w.WriteHeader(500) + } +} + +// The serverErrorResponse() method will be used when our application encounters an +// unexpected problem at runtime. +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) +} + +// The notFoundResponse() method will be used to send a 404 status code and JSON response +// to the client. +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) methodNotAllowed(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 4c0a8fa..2e7914b 100644 --- a/cmd/api/healthcheck.go +++ b/cmd/api/healthcheck.go @@ -14,8 +14,6 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques } err := app.writeJSON(w, http.StatusOK, env, nil) if err != nil { - app.logger.Debug("server error", "err", err) - http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) - return + app.serverErrorResponse(w, r, err) } } |