Docs
Error handling
Return expected errors from handlers and recover from server render failures with ErrorBoundary.
Handle expected failures in your handler or component. Use ErrorBoundary when a section can fail during server rendering and the rest of the page can still be useful.
Handle expected errors
- Invalid form input. Return field errors and re-render the form with the submitted values. See Re-rendering field errors.
- Missing resource. Return
404 Not Foundfrom the handler. Use a rendered 404 page when the response needs the site layout.
Recover from render failures
Put the normal content in ErrorBoundary’s child field and the error UI in fallback.
use proa::{DataLoader, ErrorBoundary, WebContext, WebRenderSync, WriteError, html_sync};
struct StockCount {
remaining: Result<u32, WriteError>,
}
impl<L: DataLoader> WebRenderSync<L> for StockCount {
fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
let remaining = self.remaining?;
html_sync! {
<p data-stock-count>{remaining}" in stock"</p>
}
.render(cx)
}
}
pub fn product_stock(remaining: Result<u32, WriteError>) -> impl WebRenderSync {
html_sync! {
<section aria-label="Stock">
{ErrorBoundary {
child: StockCount { remaining },
fallback: html_sync! {
<p>"Stock is temporarily unavailable."</p>
},
}}
</section>
}
}
Passing Ok(3) renders the count. Passing Err(WriteError::render("stock lookup failed")) renders the fallback. The nearest enclosing boundary handles a recoverable WriteError and discards the failed child's buffered output.
Transport failures and managed-head lifecycle errors propagate to the caller. Rust panics follow the server’s panic handling. For a failure that reaches the whole response, see Handling framework errors.
Recover from a data load failure
When a section loads its own data, call cx.load_typed inside the boundary’s child.
use std::future::Future;
use proa::{
DataLoader, ErrorBoundary, LoadKey, RenderOutcome, WebContext, WebRender, WriteError, html_sync,
};
struct LoadedStock {
sku: &'static str,
}
impl<L: DataLoader> WebRender<L> for LoadedStock {
fn render(
self,
cx: &mut WebContext<L>,
) -> RenderOutcome<impl Future<Output = Result<(), WriteError>>> {
RenderOutcome::pending(async move {
let key = LoadKey::named("stock", self.sku);
let remaining = cx.load_typed::<u32>(key).await?;
let content = html_sync! {
<p data-stock-count>{*remaining}" in stock"</p>
};
proa::WebRenderSync::render(content, cx)
})
}
}
pub fn loaded_product_stock<L: DataLoader + 'static>(sku: &'static str) -> impl WebRender<L> {
ErrorBoundary {
child: LoadedStock { sku },
fallback: html_sync! {
<p>"Stock is temporarily unavailable."</p>
},
}
}
When you render loaded_product_stock("sku_1"), the route’s loader should return LoadValue::typed(count) with a u32 count for stock:sku_1. If the loader returns a LoadError, ? converts it to a WriteError and the boundary renders fallback. See Data loading and streaming for loader and route setup.
Stream a section with a fallback
For a streamed section, put Suspense inside ErrorBoundary. Give Suspense the loading UI and ErrorBoundary the error UI. Bytes already sent stay in the response, and the HTTP status is fixed once headers are sent. See Streaming SSR.