Part 13 / Topic 1 of 5
What an error boundary catches - and what it does not
An error boundary only ever reacts to one specific thing: a throw while React is rendering one of its descendants. examples/01_render_vs_event_vs_async_errors.jsx's OrderTotalRenderCrash throws exactly there - it is handed an order whose items is null, and order.items.reduce(...) throws while the component is building the JSX it is about to return:
// examples/01_render_vs_event_vs_async_errors.jsx
export function OrderTotalRenderCrash({ order }) {
const total = order.items.reduce((sum, item) => sum + item.price, 0);
return <p>Order total: ₹{total}</p>;
}
Wrapping it in a boundary and rendering it with a malformed order really shows the boundary's fallback, not a blank screen or a crashed test run (examples/01_render_vs_event_vs_async_errors.test.jsx).
The two errors that look like they should be "the same kind of problem" are not, and this is the part of this topic people most often get wrong:
- Event handler errors.
addToCartthrows, but only when something actually calls it - from inside a click, long after React already finished rendering the button successfully:// examples/01_render_vs_event_vs_async_errors.jsx export function addToCart() { throw new Error("cart service unreachable"); }expect(() => addToCart()).toThrow("cart service unreachable")proves this is a completely ordinary synchronous throw - the exact same thing a plainonclickon a page with no React at all would do. A wrapping boundary never sees it:getDerivedStateFromErroronly runs during render, and this throw happens on the click's own call stack. (In a real browser, and in this part's own test, clicking the button surfaces it as a plain uncaught exception - the DOM spec reports a listener's throw as awindow"error"event rather than rethrowing it to whoever dispatched the click, so nothing "catches" it there either, in React or out of it. The fix is the one this course already teaches for anything risky in a handler: your owntry/catch, right there inaddToCart.) - Async errors.
placeOrderrejects, but on a later microtask, after React has already committed a perfectly successful render:
Same story:// examples/01_render_vs_event_vs_async_errors.jsx export function placeOrder() { return Promise.reject(new Error("payment gateway timeout")); }await expect(placeOrder()).rejects.toThrow(...)proves it is an ordinary rejected promise, nothing React-specific about it, and nothing a boundary watches for.
The one real technique for letting a boundary cover an async error anyway: catch it yourself, store it, and throw it back out during the very next render:
// examples/01_render_vs_event_vs_async_errors.jsx
export function PlaceOrderAsyncSurfaced() {
const [error, setError] = useState(null);
useEffect(() => {
placeOrder().catch(setError);
}, []);
if (error) throw error;
return <p>Placing order...</p>;
}
That if (error) throw error; line is a render-phase throw - the one thing a boundary actually watches for - so it reaches the boundary one render later than the request that caused it. Rendering PlaceOrderAsyncSurfaced really shows "Placing order..." first, then the boundary's fallback once the rejection lands (examples/01_render_vs_event_vs_async_errors.test.jsx).
Done here? The practice set for Part 13 — including the solutions — is on the part page.