Skip to content

Commit 4ead130

Browse files
feat(react): Document Sentry.captureReactException (#13108)
Co-authored-by: Alex Krawiec <[email protected]>
1 parent a10498b commit 4ead130

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

docs/platforms/javascript/common/usage/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Key terms:
1313
- An _event_ is one instance of sending data to Sentry. Generally, this data is an error or exception.
1414
- An _issue_ is a grouping of similar events.
1515
- The reporting of an event is called _capturing_.
16-
When an event is captured, its sent to Sentry.
16+
When an event is captured, it's sent to Sentry.
1717

1818
</Alert>
1919

docs/platforms/javascript/guides/react/features/error-boundary.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ function App({ props }) {
199199
export default App;
200200
```
201201

202+
## Manually Capturing Errors
203+
204+
If you don't want to use the Sentry Error Boundary component, you can use the `captureReactException` function to capture errors manually with your own Error Boundary. This will still ensure that the `componentStack` is linked to the error as detailed in the [Linked Errors](#linked-errors) section. The `Sentry.captureReactException` function requires React SDK `v9.8.0` or above.
205+
206+
```javascript
207+
import * as Sentry from "@sentry/react";
208+
209+
class ErrorBoundary extends React.Component {
210+
componentDidCatch(error, info) {
211+
Sentry.captureReactException(error, info);
212+
}
213+
214+
render() {
215+
return this.props.children;
216+
}
217+
}
218+
```
219+
202220
## Next Steps:
203221

204222
- [Return to **Getting Started**](../../)

docs/platforms/javascript/guides/react/index.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Select which Sentry features you'd like to install in addition to Error Monitori
2323

2424
## Install
2525

26-
<OnboardingOptionButtons options={["error-monitoring", "performance", "session-replay"]} />
26+
<OnboardingOptionButtons
27+
options={["error-monitoring", "performance", "session-replay"]}
28+
/>
2729

2830
Sentry captures data by using an SDK within your application’s runtime.
2931

@@ -45,7 +47,6 @@ Sentry supports multiple versions of React Router. To learn how to configure the
4547

4648
Sentry should be initialized as early as possible in your application. We recommend putting the Sentry initialization code into its own file and including that file as the first import in your application entry point as shown in the example below:
4749

48-
4950
```javascript {filename:instrument.js} {"onboardingOptions": {"performance": "3-8, 13-21, 24-30", "session-replay": "22, 33-39"}}
5051
import { useEffect } from "react";
5152
import * as Sentry from "@sentry/react";

platform-includes/capture-error/javascript.react.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,42 @@ function App() {
2929
// ...
3030
}
3131
```
32+
33+
The React SDK exports an error boundary component that leverages [React component APIs](https://reactjs.org/docs/error-boundaries.html) to automatically catch and send JavaScript errors from inside a React component tree to Sentry. See the [React Error Boundary](/platforms/javascript/guides/react/features/error-boundary/) guide for more information.
34+
35+
If you don't want to use the error boundary component, you can use the `captureReactException` function to capture errors manually. The `Sentry.captureReactException` function requires Sentry React SDK `v9.8.0` or above.
36+
37+
```javascript
38+
import * as Sentry from "@sentry/react";
39+
40+
class ErrorBoundary extends React.Component {
41+
componentDidCatch(error, info) {
42+
Sentry.captureReactException(error, info);
43+
}
44+
45+
render() {
46+
return this.props.children;
47+
}
48+
}
49+
```
50+
51+
Starting with React 19, the `createRoot` and `hydrateRoot` methods from `react-dom` will expose error hooks that are used to capture errors automatically. To customize how errors are handled in specific error hooks, use the `Sentry.reactErrorHandler` method. The `Sentry.reactErrorHandler` method requires `v8.6.0` or above.
52+
53+
```javascript
54+
import { createRoot } from "react-dom/client";
55+
56+
const container = document.getElementById(“app”);
57+
const root = createRoot(container, {
58+
// Callback called when an error is thrown and not caught by an ErrorBoundary.
59+
onUncaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
60+
console.warn('Uncaught error', error, errorInfo.componentStack);
61+
}),
62+
// Callback called when React catches an error in an ErrorBoundary.
63+
onCaughtError: Sentry.reactErrorHandler(),
64+
// Callback called when React automatically recovers from errors.
65+
onRecoverableError: Sentry.reactErrorHandler(),
66+
});
67+
root.render();
68+
```
69+
70+
These hooks apply to all React components that are mounted to the container which is passed onto `createRoot`/`hydrateRoot`. For more precise control over error handling, we recommend adding an `ErrorBoundary` component to your application.

0 commit comments

Comments
 (0)