Skip to content

Commit 027ebad

Browse files
committed
Improve error boundary docs
1 parent 2c5de7f commit 027ebad

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed

src/routes/concepts/control-flow/error-boundary.mdx

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,39 @@ title: "Error boundary"
33
order: 5
44
---
55

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

9-
```jsx
10-
import { ErrorBoundary } from "solid-js";
11-
12-
<ErrorBoundary fallback={(err) => <div>Error: {err.message}</div>}>
13-
<ProblematicComponent />
14-
</ErrorBoundary>
15-
```
9+
The [`<ErrorBoundary>`](/reference/components/error-boundary) component is used to create an error boundary.
10+
`<ErrorBoundary>` catches any error that occurs during the rendering or updating of its children.
11+
However, it's important to note that errors occurring outside the rendering process, such as in event handlers or after a `setTimeout`, are **not** caught by error boundaries.
1612

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

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

25-
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.
21+
function App() {
22+
return (
23+
<div>
24+
<Header />
25+
<ErrorBoundary
26+
fallback={(error, reset) => (
27+
<div>
28+
<p>Something went wrong: {error.message}</p>
29+
<button onClick={reset}>Try Again</button>
30+
</div>
31+
)}
32+
>
33+
<ErrorProne />
34+
</ErrorBoundary>
35+
</div>
36+
);
37+
}
38+
```
2639

27-
When an error is encountered, the `<ErrorBoundary>` component will catch the error and render the fallback UI instead of the problematic component(s).
28-
This way, even when a component fails, the user has a controlled UI response instead of a broken interface.
40+
In this example, when the `ErrorProne` component throws an error, the `<ErrorBoundary>` catches it, preventing it from affecting the rest of the application.
41+
Instead, it displays an error message in place of the `ErrorProne` component.

src/routes/reference/components/error-boundary.mdx

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,51 @@ title: <ErrorBoundary>
33
order: 5
44
---
55

6-
Catches uncaught errors and renders fallback content.
6+
The `<ErrorBoundary>` component catches errors that occur during the rendering or updating of its children, and shows a fallback UI instead.
7+
That includes:
78

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

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

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

20-
```tsx
21-
<ErrorBoundary fallback={<div>Something went terribly wrong</div>}>
22-
<MyComp />
23-
</ErrorBoundary>
24-
```
19+
## Props
2520

26-
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.
21+
### `fallback`
2722

28-
```tsx
29-
<ErrorBoundary
30-
fallback={(err, reset) => <div onClick={reset}>Error: {err.toString()}</div>}
31-
>
32-
<MyComp />
33-
</ErrorBoundary>
34-
```
23+
**Type**: `JSX.Element | ((err: any, reset: () => void) => JSX.Element)`
3524

36-
## Props
25+
The content to display when an error occurs.
26+
If `fallback` is a function, it receives two parameters:
27+
28+
- `err`: The caught error object.
29+
- `reset`: A function that forces the `<ErrorBoundary>` to re-render its children and clear the error state.
30+
31+
It's important to note that errors occurring within the fallback itself are not caught by the same `<ErrorBoundary>`, and will propagate to any parent error boundaries.
3732

38-
| Name | Type | Description |
39-
| :--------- | :-------------------------------------------------------------- | :------------------------------------------------------ |
40-
| `fallback` | `JSX.Element \| ((err: any, reset: () => void) => JSX.Element)` | The fallback content to render when an error is caught. |
33+
## Example
34+
35+
```tsx
36+
import { ErrorBoundary } from "solid-js";
37+
import { ErrorProne } from "./components";
38+
39+
function Example() {
40+
return (
41+
<ErrorBoundary
42+
fallback={(error, reset) => (
43+
<div>
44+
<p>{error.message}</p>
45+
<button onClick={reset}>Try Again</button>
46+
</div>
47+
)}
48+
>
49+
<ErrorProne />
50+
</ErrorBoundary>
51+
);
52+
}
53+
```

0 commit comments

Comments
 (0)