feat(errors,router): add Endpoint field/data to all errors

This commit is contained in:
Guz
2024-10-22 20:34:12 -03:00
parent 1e00971f62
commit 012d0b3137
3 changed files with 16 additions and 0 deletions

View File

@@ -13,6 +13,12 @@ templ (p ErrorPage) Component(err rerrors.RouteError) {
<main>
<h1>Error</h1>
<p>{ fmt.Sprintf("%#v", err) }</p>
for k, v := range err.Info {
<p>{ k } { fmt.Sprint(v) } </p>
}
if err.Endpoint != "" {
<a href={ templ.SafeURL(err.Endpoint) }>Retry</a>
}
</main>
}
}

View File

@@ -20,6 +20,7 @@ func Routes(logger *slog.Logger) router.Router {
if r.URL.Path != "/" {
rerrors.NotFound().ServeHTTP(w, r)
}
})
return r

View File

@@ -20,6 +20,7 @@ type RouteError struct {
StatusCode int `json:"status_code"`
Error string `json:"error"`
Info map[string]any `json:"info"`
Endpoint string
}
func NewRouteError(status int, error string, info ...map[string]any) RouteError {
@@ -112,6 +113,14 @@ func (h ErrorDisplayer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if rerr.Endpoint == "" {
q := r.URL.Query()
q.Del("error")
r.URL.RawQuery = q.Encode()
rerr.Endpoint = r.URL.String()
}
w.WriteHeader(rerr.StatusCode)
if err := h.page(rerr).Render(r.Context(), w); err != nil {
_, _ = w.Write(e)