feat: logger, middleware

This commit is contained in:
2026-03-24 22:50:13 +01:00
parent 8e9dc0b581
commit 7d81d1505a
4 changed files with 54 additions and 11 deletions
+5 -2
View File
@@ -15,14 +15,17 @@ func (app *application) failedValidationResponse(w http.ResponseWriter, r *http.
// book we'll upgrade this to use structured logging, and record additional information
// about the request including the HTTP method and URL.
func (app *application) logError(r *http.Request, err error) {
app.logger.Println(err)
app.logger.PrintError(err, map[string]string{
"request_method": r.Method,
"request_url": r.URL.String(),
})
}
// The errorResponse() method is a generic helper for sending JSON-formatted error
// messages to the client with a given status code. Note that we're using an interface{}
// type for the message parameter, rather than just a string type, as this gives us
// more flexibility over the values that we can include in the response.
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message interface{}) {
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 this happens to return an