2022-08-16 16:35:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-17 23:14:05 +02:00
|
|
|
// The logError() method is a generic helper for logging an error message.
|
2022-08-16 16:35:45 +02:00
|
|
|
func (app *application) logError(r *http.Request, err error) {
|
2022-08-16 18:05:55 +02:00
|
|
|
app.Logger.Errorw("Error",
|
|
|
|
"Request URI", r.RequestURI,
|
|
|
|
"error", err,
|
|
|
|
)
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message interface{}) {
|
|
|
|
|
|
|
|
// Write the response using the writeJSON() helper. If this happens to return an
|
|
|
|
// error then log it, and fall back to sending the client an empty response with a
|
|
|
|
// 500 Internal Server Error status code.
|
2022-08-16 18:05:55 +02:00
|
|
|
fmt.Fprintf(w, "Error: %d", status)
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
2022-08-17 15:32:42 +02:00
|
|
|
|
|
|
|
func (app *application) badRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2022-08-17 18:07:50 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-08-17 20:57:25 +02:00
|
|
|
|
|
|
|
func (app *application) editConflictResponse(w http.ResponseWriter, r *http.Request) {
|
|
|
|
message := "unable to update the record due to an edit conflict, please try again"
|
|
|
|
app.errorResponse(w, r, http.StatusConflict, message)
|
|
|
|
}
|
2022-08-17 23:14:05 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|