feat(errors,router): add Endpoint field/data to all errors
This commit is contained in:
@@ -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>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func Routes(logger *slog.Logger) router.Router {
|
||||
if r.URL.Path != "/" {
|
||||
rerrors.NotFound().ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return r
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user