40 lines
956 B
Go
40 lines
956 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
"slices"
|
|
)
|
|
|
|
var nonDebugErrors []int = []int{
|
|
http.StatusNotFound,
|
|
http.StatusBadRequest,
|
|
http.StatusTeapot,
|
|
http.StatusUnauthorized,
|
|
}
|
|
|
|
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|
sendError(w, http.StatusNotFound, "That page could not be found.")
|
|
}
|
|
|
|
func sendError(w http.ResponseWriter, code int, message string) {
|
|
tmpl := template.Must(template.ParseFiles(filepath.Join(contentDir, "templates/layout.gohtml"), filepath.Join(contentDir, "templates/error.gohtml")))
|
|
var data PageData
|
|
if *debug || slices.Contains(nonDebugErrors, code) {
|
|
data = PageData{
|
|
Header: config.HeaderData,
|
|
StatusCode: code,
|
|
StatusMessage: message,
|
|
}
|
|
} else {
|
|
data = PageData{
|
|
Header: config.HeaderData,
|
|
StatusCode: 0,
|
|
StatusMessage: "An unknown error has occurred.",
|
|
}
|
|
}
|
|
w.WriteHeader(code)
|
|
tmpl.ExecuteTemplate(w, "layout", data)
|
|
}
|