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

32
main.go
View File

@@ -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 != "" {

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
}