Skip to content

Commit

Permalink
docs(react): add error boundary docs for react-router
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome committed Sep 11, 2024
1 parent b916cd0 commit bc9d142
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/platforms/javascript/guides/react/features/react-router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,55 @@ You can instrument [`createMemoryRouter`](https://reactrouter.com/en/main/router

</Alert>

### Custom Error Boundaries

When using `react-router`, errors thrown inside route elements are only re-thrown in **development mode**. In production, these errors won't be surfaced unless manually captured. If you **don't** have a custom error boundary in place, `react-router` will create a default one that "swallows" all errors.

<Alert>
Note that this only applies to render method and lifecycle errors, since React doesn't need error boundaries to handle errors in event handlers.
</Alert>

To send errors to Sentry when using a custom error boundary, use the `Sentry.captureException` method:

```jsx {11, 27}
// router setup
const sentryCreateBrowserRouter = wrapCreateBrowserRouter(createBrowserRouter);
const router = sentryCreateBrowserRouter([
{
path: "/",
element: <YourLayout />,
children: [
{
path: "",
element: <Outlet />,
errorElement: <YourCustomRootErrorBoundary />,
children: [
// other routes ...
],
},
],
},
]);

// error boundary
import { useRouteError } from "react-router-dom";
import * as Sentry from "@sentry/react";

export function YourCustomRootErrorBoundary() {
const error = useRouteError() as Error;

Sentry.captureException(error);

return (
<div>
<h1>Ouch!</h1>
</div>
);
}

```


### Usage With `<Routes />` Component

If you're using the `<Routes />` component from `react-router-dom` to define your routes, wrap [`Routes`](https://reactrouter.com/en/main/components/routes) using `Sentry.withSentryReactRouterV6Routing`. This creates a higher order component, which will enable Sentry to reach your router context. You can also use `Sentry.withSentryReactRouterV6Routing` for `Routes` inside `BrowserRouter`. `MemoryRouter`, and `HashRouter` components:
Expand Down

0 comments on commit bc9d142

Please sign in to comment.