Skip to content

Commit f8908b3

Browse files
amirhhashemikodiakhq[bot]LadyBluenotes
authored
Improve error boundary docs (#1100)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Sarah <gerrardsarah@gmail.com>
1 parent f134d9f commit f8908b3

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-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 an 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+
It catches any error that occurs during the rendering or updating of its children.
11+
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.
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+
The `fallback` prop can be used to display a user-friendly error message or notification when an error occurs.
14+
If a function is passed 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 reset 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 the error message passed to the fallback prop.

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

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,52 @@ 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+
This 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+
`fallback` provides content to display when an error occurs.
26+
If a function is passed, 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+
If there's an error within the `fallback` itself, however, it is not caught by the same `<ErrorBoundary>`.
32+
Instead, it will bubble up to any parent error boundaries.
3733

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. |
34+
## Example
35+
36+
```tsx
37+
import { ErrorBoundary } from "solid-js";
38+
import { ErrorProne } from "./components";
39+
40+
function Example() {
41+
return (
42+
<ErrorBoundary
43+
fallback={(error, reset) => (
44+
<div>
45+
<p>{error.message}</p>
46+
<button onClick={reset}>Try Again</button>
47+
</div>
48+
)}
49+
>
50+
<ErrorProne />
51+
</ErrorBoundary>
52+
);
53+
}
54+
```

0 commit comments

Comments
 (0)