trixie/errors.go
Elara6331 2b23f0caef
Some checks failed
ci/woodpecker/tag/manifest unknown status
ci/woodpecker/tag/build/1 Pipeline failed
ci/woodpecker/tag/build/2 Pipeline failed
Initial Commit
2025-05-16 16:39:55 +02:00

31 lines
719 B
Go

package main
import (
"log/slog"
"net/http"
"time"
"go.elara.ws/salix"
)
// handleErr responds with an error page if an error is returned by fn
func handleErr(ns *salix.Namespace, fn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
err := fn(w, r)
if err != nil {
log.Error("Error in HTTP handler",
slog.String("path", r.URL.Path),
slog.Any("error", err),
)
ns.ExecuteTemplate(w, "error.html", map[string]any{
"errMsg": err.Error(),
})
}
log.Info("Request completed",
slog.String("for", r.RemoteAddr),
slog.Duration("latency", time.Since(start)),
)
})
}