ncDocConverter/internal/frontend/helpers.go

32 lines
1.1 KiB
Go

package frontend
import (
"fmt"
"net/http"
"runtime/debug"
"rpjosh.de/ncDocConverter/pkg/logger"
)
// The serverError helper writes an error message and stack trace to the errorLog,
// then sends a generic 500 Internal Server Error response to the user.
func (app *Frontend) serverError(w http.ResponseWriter, err error) {
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
logger.Error(trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
// The clientError helper sends a specific status code and corresponding description
// to the user. We'll use this later in the book to send responses like 400 "Bad
// Request" when there's a problem with the request that the user sent.
func (app *Frontend) clientError(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}
// For consistency, we'll also implement a notFound helper. This is simply a
// convenience wrapper around clientError which sends a 404 Not Found response to
// the user.
func (app *Frontend) notFound(w http.ResponseWriter) {
app.clientError(w, http.StatusNotFound)
}