One book in the catalogue arrives from the server with no price. Its card fails to draw, and what's left on screen is an empty page, including the other cards whose data is fine. One bad record takes everything down.
<ErrorBoundary> stops that. It's a safety net for the components inside it: if one of them throws an error while it's being rendered, a stand-in view (fallback) shows, not an empty page. If you come from React, the idea matches its error boundaries, only in Solid it's a built-in component you just use. Think of one shelf light in Nadia's Bookshop shorting out. A sensible shop doesn't cut the power to the whole shop: just that shelf gets a "This shelf is under repair" sign, and the other shelves stay open.
Without an ErrorBoundary: one broken card, an empty page
The This Earth of Mankind data arrives with no price:
function BookCard(props) {
const price = "$" + props.book.price.toLocaleString("en-US")
return (
<div class="card">
<h3>{props.book.title}</h3>
<p>{price}</p>
</div>
)
}
function App() {
const books = [
{ title: "The Rainbow Troops", price: 18 },
{ title: "This Earth of Mankind" },
]
return (
<div>
<BookCard book={books[0]} />
<BookCard book={books[1]} />
</div>
)
}
Tested in Solid 1.9.17: props.book.price is undefined, so .toLocaleString stops with TypeError: Cannot read properties of undefined (reading 'toLocaleString'). That error climbs all the way to render(), and the page is empty, the card for The Rainbow Troops with its correct data included.
ErrorBoundary and fallback(err, reset)
The basic shape:
import { ErrorBoundary } from "solid-js"
function App() {
return (
<ErrorBoundary fallback={(err, reset) => (
<div>
<p>Error: {err.message}</p>
<button onClick={reset}>Try again</button>
</div>
)}>
<p>Normal content</p>
</ErrorBoundary>
)
}
<ErrorBoundary>has an opening and a closing tag, because it wraps other components.- As long as its content has no error, the content shows:
<p>Normal content</p>. The fallback isn't made at all. fallbackholds a function that receives two things:err, the caught error (its message is inerr.message), andreset, a function that builds the boundary's content again.fallbackcan also hold plain JSX,fallback={<p>Failed to load</p>}, when you need neithererrnorreset.
In the shop, every card gets a boundary of its own:
<For each={books}>
{(b) => (
<ErrorBoundary fallback={(err, reset) => (
<div class="failed">
<p>Card failed: {err.message}</p>
<button onClick={reset}>Try again</button>
</div>
)}>
<BookCard book={b} />
</ErrorBoundary>
)}
</For>
| Card | What shows |
|---|---|
| The Rainbow Troops | the card, $18 |
| This Earth of Mankind | Card failed: Cannot read properties of undefined (reading 'toLocaleString') and a Try again button |
One boundary outside the <For> catches it too, but its fallback replaces every card at once. A boundary per card only covers the broken one.
reset only draws again. If the data still has no price, the card fails again with the same message. Fix the data first, say once the server sends the price, and then Try again shows $24.
What isn't caught
An ErrorBoundary catches errors that happen while drawing the components inside it: in a component's body, in its JSX, and in its createEffect or createMemo. Reading a failed createResource in JSX throws its error right there too, so that gets caught as well.
What is not caught:
- Event handlers.
onClick={() => { throw new Error("Out of stock") }}runs long after the card shows, when the button is clicked. The error shows up asUncaught Error: Out of stockin the console, and the card keeps showing as usual. - Async code. An error inside
setTimeout, or after anawaitin a handler, happens outside the drawing.
Errors like those are handled yourself with try { ... } catch (e) { ... }, then the message is kept in a signal so it shows.
⚠️ A boundary that wraps nothing, and errors that slip past
- Wrapping with a self-closing tag.
<ErrorBoundary fallback={...} />wraps nothing. The exercise checks for<ErrorBoundaryand</ErrorBoundary>. - A fallback with no button, or a button that doesn't call
reset. The exercise clicksLoad price, thenTry again, and the card has to show: the button sits in the fallback withonClick={reset}. - Thinking
resetfixes the error. It only draws the content again. If the cause is still there, the fallback comes back. - Relying on the boundary for clicks. An error in
onClickslips past the boundary. Catch it withtry/catchin the handler.
TL;DR: <ErrorBoundary fallback={(err, reset) => ...}>...</ErrorBoundary> wraps components that can fail. While the content is fine, the content shows (Normal content). If it throws while being rendered, the stand-in view shows: err.message holds the message, reset draws the content again (which fails again if the cause isn't fixed). Without a boundary, one broken card empties the whole page. A boundary catches errors in a component's body, JSX, effects, and resources read in JSX, but not errors in event handlers or in async code.
⚡ One broken card, one empty page
Guess first: the This Earth of Mankind data arrives with no price, so its card throws a TypeError while drawing. Does the card for The Rainbow Troops, whose data is fine, still show?
No. The error climbs all the way to render(), and the whole page is empty, the card for The Rainbow Troops included.
Meaning: Without a safety net, one bad record takes everything down.
⚡ ErrorBoundary and fallback(err, reset)
Write:
<ErrorBoundary fallback={(err, reset) => (
<div>
<p>Error: {err.message}</p>
<button onClick={reset}>Try again</button>
</div>
)}>
<p>Normal content</p>
</ErrorBoundary>
Meaning: While its content has no error, the content shows. If the content throws while being drawn, the fallback shows: err.message holds the message, and reset draws the content again. This exercise checks for an opening and a closing tag, so don't write <ErrorBoundary />.
⚡ What isn't caught
Meaning: reset only draws again, so if the cause is still there, the fallback comes back. Errors in event handlers, such as onClick, and in async code aren't caught by a boundary. Handle them yourself with try { ... } catch (e) { ... }.