make error pages dynamic
1 file changed, 5 insertions(+), 10 deletions(-)
changed files
M internal/website/mux.go → internal/website/mux.go
@@ -6,6 +6,7 @@ "net/http" "path" "strings" "website/internal/config" + ihttp "website/internal/http" "website/internal/log" "website/templates"@@ -14,12 +15,6 @@ "github.com/kevinpollet/nego" "github.com/pkg/errors" ) -type HTTPError struct { - Error error - Message string - Code int -} - func canonicalisePath(path string) (cPath string, differs bool) { cPath = path if strings.HasSuffix(path, "/index.html") {@@ -31,7 +26,7 @@ return cPath, differs } -type webHandler func(http.ResponseWriter, *http.Request) *HTTPError +type webHandler func(http.ResponseWriter, *http.Request) *ihttp.Error type WrappedWebHandler struct { config *config.Config@@ -55,7 +50,7 @@ }() if err := fn.handler(w, r); err != nil { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - err := templates.NotFound(*fn.config, r.URL.Path).Render(r.Context(), w) + err := templates.Error(*fn.config, r.URL.Path, err).Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }@@ -76,7 +71,7 @@ return nil, errors.WithMessagef(err, "registering content files") } templates.Setup() - mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *HTTPError { + mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *ihttp.Error { urlPath, shouldRedirect := canonicalisePath(r.URL.Path) if shouldRedirect { http.Redirect(w, r, urlPath, 302)@@ -85,7 +80,7 @@ return nil } file := GetFile(urlPath) if file == nil { - return &HTTPError{ + return &ihttp.Error{ Message: "File not found", Code: http.StatusNotFound, }