fix errors

This commit is contained in:
orangix
2026-01-23 20:14:56 +01:00
parent 975ffa0b9c
commit 4441d25d38
3 changed files with 49 additions and 18 deletions

View File

@@ -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
}