Docs

Error handling

Return expected errors from handlers and recover from server render failures with ErrorBoundary.

Open Markdown

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

Recover from render failures

Put the normal content in ErrorBoundary’s child field and the error UI in fallback.

src/components/stock.rs
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.

src/components/loaded_stock.rs
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.

Search

Type at least 2 characters