mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 17:41:13 +00:00
44 lines
909 B
Go
44 lines
909 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"codeberg.org/rimgo/rimgo/render"
|
|
"codeberg.org/rimgo/rimgo/static"
|
|
)
|
|
|
|
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") != "" {
|
|
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)
|
|
|
|
} else {
|
|
w.WriteHeader(code)
|
|
err = render.Render(w, "errors/"+codeStr, map[string]any{
|
|
"path": r.URL.Path,
|
|
"err": str[0],
|
|
})
|
|
}
|
|
if err != nil {
|
|
// don't panic or return error, it will loop
|
|
fmt.Println("error in RenderError: " + err.Error())
|
|
}
|
|
return nil
|
|
}
|