diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-06 11:48:32 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-06 11:51:12 +0100 |
commit | e7e517b822333188ea34f4a4ba83de2253cfe869 (patch) | |
tree | 7c467051ec95e13e1db3925bf80c8b27c40a97db /cmd/api/errors.go | |
parent | a931a4637f6d51de4059df6d0d72d147e4775018 (diff) |
Reorganises cmd/ structure for publication on pkg.go.dev
Adds key commands to Makefile
Diffstat (limited to 'cmd/api/errors.go')
-rw-r--r-- | cmd/api/errors.go | 63 |
1 files changed, 0 insertions, 63 deletions
diff --git a/cmd/api/errors.go b/cmd/api/errors.go deleted file mode 100644 index 5f5122a..0000000 --- a/cmd/api/errors.go +++ /dev/null @@ -1,63 +0,0 @@ -// dbasik provides a service with which to convert spreadsheets containing -// data to JSON for further processing. - -// Copyright (C) 2024 M R Lemon - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. -package main - -import ( - "fmt" - "net/http" -) - -// 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) -} |