BrainMinder/cmd/web/errors.go

36 lines
950 B
Go
Raw Normal View History

2024-08-22 10:13:16 +02:00
package main
import (
"log/slog"
"net/http"
"runtime/debug"
)
func (app *application) reportServerError(r *http.Request, err error) {
var (
message = err.Error()
method = r.Method
url = r.URL.String()
trace = string(debug.Stack())
)
requestAttrs := slog.Group("request", "method", method, "url", url)
app.logger.Error(message, requestAttrs, "trace", trace)
}
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
app.reportServerError(r, err)
message := "The server encountered a problem and could not process your request"
http.Error(w, message, http.StatusInternalServerError)
}
func (app *application) notFound(w http.ResponseWriter, r *http.Request) {
message := "The requested resource could not be found"
http.Error(w, message, http.StatusNotFound)
}
func (app *application) badRequest(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), http.StatusBadRequest)
}