31 lines
719 B
Go
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)),
|
|
)
|
|
})
|
|
}
|