Create go backend for site SSR and Dockerfile for dockerization
This commit is contained in:
39
error_handling.go
Normal file
39
error_handling.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user