A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
1.2 KiB

package responses
import (
"log"
"net/http"
)
// Panic gives a simple error with no template or anything fancy.
func Panic(w http.ResponseWriter, code int, message string) {
w.WriteHeader(code)
w.Write([]byte(message))
}
// Error returns an error page.
func Error(w http.ResponseWriter, r *http.Request, code int, message string) {
v := map[string]interface{}{
"Message": message,
}
w.WriteHeader(code)
if err := RenderTemplate(w, r, "_builtin/errors/generic.gohtml", v); err != nil {
log.Printf("responses.Error: failed to render a pretty error template: %s", err)
w.Write([]byte(message))
}
}
// NotFound returns an HTML 404 page.
func NotFound(w http.ResponseWriter, r *http.Request) {
if err := RenderTemplate(w, r, "_builtin/errors/404.gohtml", nil); err != nil {
log.Printf("responses.NotFound: failed to render a pretty error template: %s", err)
Panic(w, http.StatusNotFound, "Not Found")
}
}
// BadRequest returns a 400 Bad Request page.
func BadRequest(w http.ResponseWriter, r *http.Request, vars interface{}) {
if err := RenderTemplate(w, r, "_builtin/errors/generic.gohtml", vars); err != nil {
log.Printf("responses.NotFound: failed to render a pretty error template: %s", err)
Panic(w, http.StatusBadRequest, "Bad Request")
}
}