From 100d0f02f61d4882105a24810f3e7df497435746 Mon Sep 17 00:00:00 2001 From: Ben Garrett Date: Mon, 30 Oct 2023 14:03:45 +1100 Subject: [PATCH] AboutErr 404 page for invalid files. --- doc.go | 1 - handler/app/render_about.go | 5 ++++- handler/app/render_err.go | 27 ++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/doc.go b/doc.go index 503af2a8..66472722 100644 --- a/doc.go +++ b/doc.go @@ -91,7 +91,6 @@ The following options can be added to [Override]. # Bugs - `public/text/osd.xml` needs updating to use current search links. - - /f/{{bad id}} currently returns a 500 error, should return a 404. - also check groups for the same problem. - ren x_pression to x_pression-design diff --git a/handler/app/render_about.go b/handler/app/render_about.go index c26aaa9b..14e7ec4c 100644 --- a/handler/app/render_about.go +++ b/handler/app/render_about.go @@ -39,7 +39,10 @@ func (a AboutConf) About(z *zap.SugaredLogger, c echo.Context) error { } res, err := model.OneRecord(z, c, a.URI) if err != nil { - return DatabaseErr(z, c, "f/"+a.URI, ErrZap) + if errors.Is(err, model.ErrID) { + return AboutErr(z, c, a.URI) + } + return DatabaseErr(z, c, "f/"+a.URI, err) } fname := res.Filename.String uuid := res.UUID.String diff --git a/handler/app/render_err.go b/handler/app/render_err.go index 6a18fe8b..8f8dee7b 100644 --- a/handler/app/render_err.go +++ b/handler/app/render_err.go @@ -23,13 +23,38 @@ import ( "go.uber.org/zap" ) +// AboutErr renders the about file error page for the the About files links. +func AboutErr(z *zap.SugaredLogger, c echo.Context, id string) error { + const name = "status" + if z == nil { + return InternalErr(z, c, name, ErrZap) + } + if c == nil { + return InternalErr(z, c, name, ErrCxt) + } + data := empty() + data["title"] = fmt.Sprintf("%d error, file about page not found", http.StatusNotFound) + data["description"] = fmt.Sprintf("HTTP status %d error", http.StatusNotFound) + data["code"] = http.StatusNotFound + data["logo"] = "About file not found" + data["alert"] = fmt.Sprintf("About file %q cannot be found", strings.ToLower(id)) + data["probl"] = "The about file page does not exist, there is probably a typo with the URL." + data["uriOkay"] = "f/" + data["uriErr"] = id + err := c.Render(http.StatusNotFound, name, data) + if err != nil { + return InternalErr(z, c, name, err) + } + return nil +} + // DatabaseErr is the handler for handling database connection errors. func DatabaseErr(z *zap.SugaredLogger, c echo.Context, uri string, err error) error { const code = http.StatusInternalServerError if z == nil { zapNil(err) } else if err != nil { - z.Warnf("%d error for %q: %s", code, uri, err) + z.Errorf("%d error for %q: %s", code, uri, err) } // render the fallback, text only error page if c == nil {