Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
027ebad
Improve error boundary docs
amirhhashemi Feb 27, 2025
78db858
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Feb 27, 2025
f93746a
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 4, 2025
4f49724
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 4, 2025
c9f3d2d
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 6, 2025
e467d11
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 7, 2025
7c32382
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 7, 2025
0f34e2e
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 7, 2025
dd85eb1
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 7, 2025
726a321
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 7, 2025
3b72b1f
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 8, 2025
e4496ca
Merge branch 'main' into improve-error-boundary-docs
kodiakhq[bot] Mar 8, 2025
cdc713d
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
1623422
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
bc5cb04
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
78be42d
Update src/routes/reference/components/error-boundary.mdx
amirhhashemi Mar 8, 2025
88b254e
Update src/routes/reference/components/error-boundary.mdx
amirhhashemi Mar 8, 2025
c32c834
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
998a751
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
5138086
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
746f771
Update src/routes/concepts/control-flow/error-boundary.mdx
amirhhashemi Mar 8, 2025
e018fd1
Update src/routes/reference/components/error-boundary.mdx
amirhhashemi Mar 8, 2025
37d7c99
Update src/routes/reference/components/error-boundary.mdx
amirhhashemi Mar 8, 2025
d074228
fix grammar and typos and format
amirhhashemi Mar 8, 2025
f57b279
Merge branch 'main' into improve-error-boundary-docs
LadyBluenotes Mar 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions src/routes/concepts/control-flow/error-boundary.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@ title: "Error boundary"
order: 5
---

`<ErrorBoundary>` is a component that can be used to catch errors thrown by child components.
When encountering an error, this component will render a fallback UI instead of the problematic child component(s).
By default, if part of an application throws an error during rendering, the entire application can crash, resulting in Solid removing its UI from the screen.
Error boundaries provide a way to catch these errors and prevent the entire app from crashing.

```jsx
import { ErrorBoundary } from "solid-js";

<ErrorBoundary fallback={(err) => <div>Error: {err.message}</div>}>
<ProblematicComponent />
</ErrorBoundary>
```
The [`<ErrorBoundary>`](/reference/components/error-boundary) component is used to create an error boundary.
It catches any error that occurs during the rendering or updating of its children.
However, an important note is that errors occurring outside the rendering process, such as in event handlers or after a `setTimeout`, are _not_ caught by error boundaries.

`<ErrorBoundary>` accepts a `fallback` prop that can be used to render a custom error message, or to provide a friendly notification to the user.
This prop accepts a function that receives the caught error as an argument, providing a flexible way to handle different error scenarios.
The `fallback` prop can be used to display a user-friendly error message or notification when an error occurs.
If a function is passed to `fallback`, it will receive the error object as well as a `reset` function.
The `reset` function forces the `<ErrorBoundary>` to re-render its children and reset the error state, providing users with a way to recover from the error.

<EraserLink
href="https://app.eraser.io/workspace/maDvFw5OryuPJOwSLyK9?elements=aSw5yYrSY22mI_YqoZlpGQ"
preview="https://app.eraser.io/workspace/maDvFw5OryuPJOwSLyK9/preview?elements=aSw5yYrSY22mI_YqoZlpGQ&type=embed"
/>
```tsx
import { ErrorBoundary } from "solid-js";
import { Header, ErrorProne } from "./components";

By wrapping parts of your application in `<ErrorBoundary>`, you can prevent the entire application from crashing when an error occurs due to a single component.
function App() {
return (
<div>
<Header />
<ErrorBoundary
fallback={(error, reset) => (
<div>
<p>Something went wrong: {error.message}</p>
<button onClick={reset}>Try Again</button>
</div>
)}
>
<ErrorProne />
</ErrorBoundary>
</div>
);
}
```

When an error is encountered, the `<ErrorBoundary>` component will catch the error and render the fallback UI instead of the problematic component(s).
This way, even when a component fails, the user has a controlled UI response instead of a broken interface.
In this example, when the `ErrorProne` component throws an error, the `<ErrorBoundary>` catches it, preventing it from affecting the rest of the application.
Instead, it displays the error message passed to the fallback prop.
68 changes: 41 additions & 27 deletions src/routes/reference/components/error-boundary.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,52 @@ title: <ErrorBoundary>
order: 5
---

Catches uncaught errors and renders fallback content.
The `<ErrorBoundary>` component catches errors that occur during the rendering or updating of its children and shows a fallback UI instead.
This includes:

```tsx
import { ErrorBoundary } from "solid-js"
import type { JSX } from "solid-js"
- Errors that occur while rendering JSX.
- Errors that occur within `createEffect`, `createMemo`, and other state management primitives.
- Errors that occur within `createResource` and other asynchronous state management or data-fetching primitives.

function ErrorBoundary(props: {
fallback: JSX.Element | ((err: any, reset: () => void) => JSX.Element)
children: JSX.Element
}): () => JSX.Element
```
However, errors occurring outside the rendering process are **not** captured by error boundaries.
For instance:

Here's an example of how to use it:
- Errors that occur inside event handlers.
- Errors that occur after a `setTimeout`.

```tsx
<ErrorBoundary fallback={<div>Something went terribly wrong</div>}>
<MyComp />
</ErrorBoundary>
```
## Props

If you want to customize the error message, you can pass a function as the `fallback` prop. The function will be called with the error and a `reset` function. The `reset` function will reset the error boundary and re-render the children.
### `fallback`

```tsx
<ErrorBoundary
fallback={(err, reset) => <div onClick={reset}>Error: {err.toString()}</div>}
>
<MyComp />
</ErrorBoundary>
```
**Type**: `JSX.Element | ((err: any, reset: () => void) => JSX.Element)`

## Props
`fallback` provides content to display when an error occurs.
If a function is passed, it receives two parameters:

- `err`: The caught error object.
- `reset`: A function that forces the `<ErrorBoundary>` to re-render its children and clear the error state.

If there's an error within the `fallback` itself, however, it is not caught by the same `<ErrorBoundary>`.
Instead, it will bubble up to any parent error boundaries.

| Name | Type | Description |
| :--------- | :-------------------------------------------------------------- | :------------------------------------------------------ |
| `fallback` | `JSX.Element \| ((err: any, reset: () => void) => JSX.Element)` | The fallback content to render when an error is caught. |
## Example

```tsx
import { ErrorBoundary } from "solid-js";
import { ErrorProne } from "./components";

function Example() {
return (
<ErrorBoundary
fallback={(error, reset) => (
<div>
<p>{error.message}</p>
<button onClick={reset}>Try Again</button>
</div>
)}
>
<ErrorProne />
</ErrorBoundary>
);
}
```
Loading