From 4441d25d38a56d0396f5892958d501397d9498c1 Mon Sep 17 00:00:00 2001 From: orangix Date: Fri, 23 Jan 2026 20:14:56 +0100 Subject: [PATCH] fix errors --- main.go | 32 +++++++++++++++++++--- utils/error.go | 35 ++++++++++++++----------- views/errors/{error.hbs => generic.hbs} | 0 3 files changed, 49 insertions(+), 18 deletions(-) rename views/errors/{error.hbs => generic.hbs} (100%) diff --git a/main.go b/main.go index c327f31..0b99d4a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "os" "strings" "codeberg.org/rimgo/rimgo/pages" @@ -20,10 +21,15 @@ type handler func(w http.ResponseWriter, r *http.Request) error func wrapHandler(h handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + if v := recover(); v != nil { + utils.RenderError(w, r, 500, fmt.Sprint(v)) + } + }() err := h(w, r) if err != nil { fmt.Println(err) - http.Error(w, http.StatusText(500), 500) + utils.RenderError(w, r, 500, err.Error()) } }) } @@ -53,11 +59,31 @@ func main() { io.Copy(w, file) })) + if os.Getenv("ENV") == "dev" { + app.Handle("GET /errors/429", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + utils.RenderError(w, r, 429) + })) + app.Handle("GET /errors/429/img", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Location", "/static/img/error-429.png") + w.WriteHeader(302) + })) + app.Handle("GET /errors/404", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + utils.RenderError(w, r, 404) + })) + app.Handle("GET /errors/404/img", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Location", "/static/img/error-404.png") + w.WriteHeader(302) + })) + app.Handle("GET /errors/error", wrapHandler(func(w http.ResponseWriter, r *http.Request) error { + return fmt.Errorf("Test error") + })) + app.Handle("GET /errors/panic", wrapHandler(func(w http.ResponseWriter, r *http.Request) error { + panic("Test error") + })) + } app.Handle("GET /{$}", wrapHandler(pages.HandleFrontpage)) - // app.Handle("GET /{postID}/embed", wrapHandler(pages.HandleEmbed)) // fix this conflict app.Handle("GET /a/{postID}", wrapHandler(pages.HandlePost)) app.Handle("GET /a/{postID}/embed", wrapHandler(pages.HandleEmbed)) - // app.Handle("GET /t/:tag.:type", pages.HandleTagRSS) app.Handle("GET /t/{tag}", wrapHandler(func(w http.ResponseWriter, r *http.Request) error { name, ext := utils.SplitNameExt(r.PathValue("tag")) if ext != "" { diff --git a/utils/error.go b/utils/error.go index 00680d3..30bd215 100644 --- a/utils/error.go +++ b/utils/error.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "io" "net/http" "strconv" @@ -9,30 +10,34 @@ import ( "codeberg.org/rimgo/rimgo/static" ) -func RenderError(w http.ResponseWriter, r *http.Request, code int) error { +func RenderError(w http.ResponseWriter, r *http.Request, code int, str ...string) (err error) { + if len(str) != 1 { + str = []string{""} + } + codeStr := "generic" + if code == 0 { + code = 500 + } + if code != 500 { + codeStr = strconv.Itoa(code) + } if !Accepts(r, "text/html") && r.PathValue("extension") != "" { - codeStr := "generic" - if code != 0 { - codeStr = strconv.Itoa(code) - } w.Header().Set("Content-Type", "image/png") w.WriteHeader(code) file, _ := static.GetFiles().Open("img/error-" + codeStr + ".png") defer file.Close() - _, err := io.Copy(w, file) - if err != nil { - // panic on error to avoid a loop - panic(err) - } + _, err = io.Copy(w, file) + } else { w.WriteHeader(code) - err := render.Render(w, "errors/"+strconv.Itoa(code), map[string]any{ + err = render.Render(w, "errors/"+codeStr, map[string]any{ "path": r.URL.Path, + "err": str[0], }) - if err != nil { - // panic on error to avoid a loop - panic(err) - } + } + if err != nil { + // don't panic or return error, it will loop + fmt.Println("error in RenderError: " + err.Error()) } return nil } diff --git a/views/errors/error.hbs b/views/errors/generic.hbs similarity index 100% rename from views/errors/error.hbs rename to views/errors/generic.hbs