update to new webserver module

This commit is contained in:
2026-01-10 14:46:49 +11:00
parent 28b7ba34f0
commit a0cd269466
14 changed files with 199 additions and 69 deletions

View File

@@ -3,14 +3,16 @@ package handler
import (
"net/http"
"projectreshoot/internal/view/page"
"git.haelnorr.com/h/golib/hws"
"github.com/pkg/errors"
)
func ErrorPage(
errorCode int,
w http.ResponseWriter,
r *http.Request,
) {
message := map[int]string{
) (hws.ErrorPage, error) {
messages := map[int]string{
400: "The request you made was malformed or unexpected.",
401: "You need to login to view this page.",
403: "You do not have permission to view this page.",
404: "The page or resource you have requested does not exist.",
@@ -18,25 +20,9 @@ func ErrorPage(
continues to happen contact an administrator.`,
503: "The server is currently down for maintenance and should be back soon. =)",
}
w.WriteHeader(errorCode)
page.Error(errorCode, http.StatusText(errorCode), message[errorCode]).
Render(r.Context(), w)
}
func NewErrorPage(
errorCode int,
w http.ResponseWriter,
r *http.Request,
) error {
message := map[int]string{
401: "You need to login to view this page.",
403: "You do not have permission to view this page.",
404: "The page or resource you have requested does not exist.",
500: `An error occured on the server. Please try again, and if this
continues to happen contact an administrator.`,
503: "The server is currently down for maintenance and should be back soon. =)",
msg, exists := messages[errorCode]
if !exists {
return nil, errors.New("No valid message for the given code")
}
w.WriteHeader(errorCode)
return page.Error(errorCode, http.StatusText(errorCode), message[errorCode]).
Render(r.Context(), w)
return page.Error(errorCode, http.StatusText(errorCode), msg), nil
}