learn-go/snippetbox/cmd/web/helpers.go

27 lines
690 B
Go

package main
import (
"log/slog"
"net/http"
"runtime/debug"
)
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
var (
method = r.Method
trace = string(debug.Stack())
uri = r.URL.RequestURI()
)
app.logger.Error(err.Error(), slog.String("method", method), slog.String("trace", trace), slog.String("uri", uri))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func (app *application) clientError(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}
func (app *application) notFound(w http.ResponseWriter) {
app.clientError(w, http.StatusNotFound)
}