Skip to content

Commit 7bc3a2a

Browse files
committed
Docs
1 parent b8b01b3 commit 7bc3a2a

File tree

4 files changed

+99
-19
lines changed

4 files changed

+99
-19
lines changed

docs/how-to/error-boundary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: Error Boundaries
1111

1212
To avoid rendering an empty page to users, route modules will automatically catch errors in your code and render the closest `ErrorBoundary`.
1313

14-
Error boundaries are not intended for error reporting or rendering form validation errors. Please see [Form Validation](./form-validation) and [Error Reporting](./error-reporting) instead.
14+
Error boundaries are not intended for rendering form validation errors or error reporting. Please see [Form Validation](./form-validation) and [Error Reporting](./error-reporting) instead.
1515

1616
## 1. Add a root error boundary
1717

docs/how-to/error-reporting.md

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@ title: Error Reporting
44

55
# Error Reporting
66

7-
[MODES: framework]
7+
[MODES: framework,data]
88

99
<br/>
1010
<br/>
1111

12-
React Router catches errors in your route modules and sends them to [error boundaries](./error-boundary) to prevent blank pages when errors occur. However, ErrorBoundary isn't sufficient for logging and reporting errors. To access these caught errors, use the handleError export of the server entry module.
12+
React Router catches errors in your route modules and sends them to [error boundaries](./error-boundary) to prevent blank pages when errors occur. However, `ErrorBoundary` isn't sufficient for logging and reporting errors.
1313

14-
## 1. Reveal the server entry
14+
## Server Errors
1515

16-
If you don't see `entry.server.tsx` in your app directory, you're using a default entry. Reveal it with this cli command:
16+
[modes: framework]
17+
18+
To access these caught errors on the server, use the `handleError` export of the server entry module.
19+
20+
### 1. Reveal the server entry
21+
22+
If you don't see [`entry.server.tsx`][entryserver] in your app directory, you're using a default entry. Reveal it with this cli command:
1723

1824
```shellscript nonumber
19-
react-router reveal
25+
react-router reveal entry.server
2026
```
2127

22-
## 2. Export your error handler
28+
### 2. Export your error handler
2329

2430
This function is called whenever React Router catches an error in your application on the server.
2531

@@ -39,3 +45,75 @@ export const handleError: HandleErrorFunction = (
3945
}
4046
};
4147
```
48+
49+
## Client Errors
50+
51+
To access these caught errors on the client, use the `onError` prop on your [`HydratedRouter`][hydratedrouter] or [`RouterProvider`][routerprovider] component.
52+
53+
### Framework Mode
54+
55+
[modes: framework]
56+
57+
#### 1. Reveal the client entry
58+
59+
If you don't see [`entry.client.tsx`][entryclient] in your app directory, you're using a default entry. Reveal it with this cli command:
60+
61+
```shellscript nonumber
62+
react-router reveal entry.client
63+
```
64+
65+
#### 2. Add your error handler
66+
67+
This function is called whenever React Router catches an error in your application on the client.
68+
69+
```tsx filename=entry.client.tsx
70+
import { type ClientOnErrorFunction } from "react-router";
71+
72+
const onError: ClientOnErrorFunction = (
73+
error,
74+
{ location, params, unstable_pattern, errorInfo },
75+
) => {
76+
myReportError(error, location, errorInfo);
77+
78+
// make sure to still log the error so you can see it
79+
console.error(error, errorInfo);
80+
};
81+
82+
startTransition(() => {
83+
hydrateRoot(
84+
document,
85+
<StrictMode>
86+
<HydratedRouter onError={onError} />
87+
</StrictMode>,
88+
);
89+
});
90+
```
91+
92+
### Data Mode
93+
94+
[modes: data]
95+
96+
This function is called whenever React Router catches an error in your application on the client.
97+
98+
```tsx
99+
import { type ClientOnErrorFunction } from "react-router";
100+
101+
const onError: ClientOnErrorFunction = (
102+
error,
103+
{ location, params, unstable_pattern, errorInfo },
104+
) => {
105+
myReportError(error, location, errorInfo);
106+
107+
// make sure to still log the error so you can see it
108+
console.error(error, errorInfo);
109+
};
110+
111+
function App() {
112+
return <RouterProvider onError={onError} />;
113+
}
114+
```
115+
116+
[entryserver]: ../api/framework-conventions/entry.server.tsx
117+
[entryclient]: ../api/framework-conventions/entry.client.tsx
118+
[hydratedrouter]: ../api//framework-routers/HydratedRouter
119+
[routerprovider]: ../api/data-routers/RouterProvider

packages/react-router/lib/components.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,20 @@ export interface RouterProviderProps {
364364
*/
365365
flushSync?: (fn: () => unknown) => undefined;
366366
/**
367-
* An error handler function that will be called for any loader/action/render
368-
* errors that are encountered in your application. This is useful for
369-
* logging or reporting errors instead of the `ErrorBoundary` because it's not
367+
* An error handler function that will be called for any middleware, loader, action,
368+
* or render errors that are encountered in your application. This is useful for
369+
* logging or reporting errors instead of in the {@link ErrorBoundary} because it's not
370370
* subject to re-rendering and will only run one time per error.
371371
*
372372
* The `errorInfo` parameter is passed along from
373373
* [`componentDidCatch`](https://react.dev/reference/react/Component#componentdidcatch)
374374
* and is only present for render errors.
375375
*
376376
* ```tsx
377-
* <RouterProvider onError=(error, errorInfo) => {
378-
* console.error(error, errorInfo);
379-
* reportToErrorService(error, errorInfo);
377+
* <RouterProvider onError=(error, info) => {
378+
* let { location, params, unstable_pattern, errorInfo } = info;
379+
* console.error(error, location, errorInfo);
380+
* reportToErrorService(error, location, errorInfo);
380381
* }} />
381382
* ```
382383
*/

packages/react-router/lib/dom-export/hydrated-router.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,20 @@ export interface HydratedRouterProps {
281281
*/
282282
unstable_instrumentations?: unstable_ClientInstrumentation[];
283283
/**
284-
* An error handler function that will be called for any loader/action/render
285-
* errors that are encountered in your application. This is useful for
286-
* logging or reporting errors instead of the `ErrorBoundary` because it's not
284+
* An error handler function that will be called for any middleware, loader, action,
285+
* or render errors that are encountered in your application. This is useful for
286+
* logging or reporting errors instead of in the {@link ErrorBoundary} because it's not
287287
* subject to re-rendering and will only run one time per error.
288288
*
289289
* The `errorInfo` parameter is passed along from
290290
* [`componentDidCatch`](https://react.dev/reference/react/Component#componentdidcatch)
291291
* and is only present for render errors.
292292
*
293293
* ```tsx
294-
* <HydratedRouter onError={(error, errorInfo) => {
295-
* console.error(error, errorInfo);
296-
* reportToErrorService(error, errorInfo);
294+
* <HydratedRouter onError=(error, info) => {
295+
* let { location, params, unstable_pattern, errorInfo } = info;
296+
* console.error(error, location, errorInfo);
297+
* reportToErrorService(error, location, errorInfo);
297298
* }} />
298299
* ```
299300
*/

0 commit comments

Comments
 (0)