Expand description
Custom 404 / 500 page helpers.
Two pieces of AppBuilder state plug
together with the existing templates engine to deliver the
“drop a 404.html in your templates dir” experience:
not_found_template(name)— installs a fallback that renders the named template with{ path }in scope and returns 404.server_error_template(name)— wraps the router with a panic-catching tower-http layer that renders the named template on any handler panic and returns 500.
Gap 35 extensions:
on_server_error(hook)— an opt-in hook that fires before the 500 template is rendered. The closure receives the error message and the request path. Runs synchronously on the error path (panic orErrpropagated as a 500). Cannot change the response; used for logging, Sentry dispatch, etc.- Default Tailwind 404/500 templates — shipped as embedded strings so
they work without any
templates/directory on disk. Used when the user hasn’t set their own template name via the builder. Opt-out viaApp::builder().disable_default_error_pages(). - Dev-mode error detail — when
settings.environment == Dev, the 500 template receives anerror_chaincontext variable listing the fullstd::error::Errorsource chain. In prod the variable is empty.
Both the 404 and 500 fallbacks are opt-in. When unset (and default pages are disabled), the fallback returns plain-text “Not Found” and panics propagate axum-style (log + empty 500 body).
The 404 path composes with SlashRedirect
— if the redirect probe finds an alternate, it 308s; otherwise the
configured not-found template renders. Users get one consistent
404 page across normal misses and slash-redirect dead-ends.
Structs§
- Render500
State - State for the response-rendering middleware. Cloned per-request; both
fields are cheap to clone (
Option<String>+Option<Arc<...>>). - Render
Error State - State for the general error-page middleware: a status → template-name map.
Cloned per request (an
Arc, cheap).
Constants§
- DEFAULT_
404_ HTML - Default 404 page: centered, inline Tailwind utility classes. Degrades gracefully without Tailwind loaded — the page is functional even as unstyled HTML.
- DEFAULT_
404_ TEMPLATE_ NAME - DEFAULT_
500_ HTML - Default 500 page: same shape as the 404. In dev mode the template
receives
error_display,error_chain(vec of source strings), andrequest_pathcontext variables that render into an expandable detail block. In prod those variables are empty strings / empty vecs. - DEFAULT_
500_ TEMPLATE_ NAME
Functions§
- collect_
error_ chain - Walk the
std::error::Error::source()chain and collect everyDisplaymessage into aVec<String>. The first entry is the top-level error itself; subsequent entries are its causes. - fire_
server_ error_ hook - Build an axum fallback or middleware that converts a handler
Errresponse into a 500 with optional dev-mode detail and hook notification. - not_
found_ fallback - Build an axum fallback handler that renders the configured 404
template. Used when
not_found_templateis set butslash_redirectisOff—App::buildskips the slash redirect path and installs this directly. - render_
500_ middleware - Middleware that intercepts plain-text 500 responses and re-renders them
through the configured
server_error_template(or the embedded default when enabled). Already-HTML 500 responses pass through untouched — those were rendered byCatchPanicLayer(panics) or by the handler itself. - render_
error_ middleware - Middleware that styles error responses for ANY registered status code
(e.g. 429, 403, 410) the way
render_500_middlewaredoes for 500. After the handler runs, if the response status has a registered template and the body isn’t already HTML, the body text is captured as themessageand the template is rendered in its place — preserving the original status code. - render_
not_ found - Render the configured 404 template with
{ path }in scope, or fall back to the plain-text response when no template is set or rendering fails. - server_
error_ panic_ handler - Build the panic-handler closure for
tower_http::catch_panic::CatchPanicLayer::custom.
Type Aliases§
- Server
Error Hook - Shared callback type for the
on_server_errorhook.