You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.1 KiB
Go

package server
import (
"net/http"
"runtime/debug"
)
// serverError helper writes a log entry at Error level (including the request
// method and URI as attributes), then sends a generic 500 Internal Server Error
// response to the user.
func (rs *RatchetServer) serverError(w http.ResponseWriter, r *http.Request, err error) {
var (
method = r.Method
uri = r.URL.RequestURI()
// Use debug.Stack() to get the stack trace. This returns a byte slice, which
// we need to convert to a string so that it's readable in the log entry.
trace = string(debug.Stack())
)
rs.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
// 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 (rs *RatchetServer) clientError(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}