mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 09:31:13 +00:00
fix errors
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user