Skip to content

Commit

Permalink
move content about error capturing to "Capturing Errors and Events" f…
Browse files Browse the repository at this point in the history
…rom "Manual Setup" (Next.js) (#12786)

* move capture error content

* adapt formatting

* formatting fix

Co-authored-by: Charly Gomez <charly.gomez@sentry.io>

---------

Co-authored-by: Charly Gomez <charly.gomez@sentry.io>
  • Loading branch information
inventarSarah and chargome authored Feb 26, 2025
1 parent a6f1f9e commit f1bb1dd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
47 changes: 0 additions & 47 deletions docs/platforms/javascript/guides/nextjs/manual-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,53 +239,6 @@ export default function GlobalError({ error }) {
}
```

Note that if you create [Next.js error.js files](https://nextjs.org/docs/app/building-your-application/routing/error-handling), these files will take precedence over the `global-error.js` file.
This means, that if you want to report errors that are caught by `error.js` files, you need to manually capture them:

```tsx {filename:error.tsx}
"use client";

import { useEffect } from "react";
import * as Sentry from "@sentry/nextjs";

export default function ErrorPage({
error,
}: {
error: Error & { digest?: string };
}) {
useEffect(() => {
// Log the error to Sentry
Sentry.captureException(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
</div>
);
}
```

```jsx {filename:error.jsx}
"use client";

import { useEffect } from "react";
import * as Sentry from "@sentry/nextjs";

export default function ErrorPage({ error }) {
useEffect(() => {
// Log the error to Sentry
Sentry.captureException(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
</div>
);
}
```

#### Errors from Nested React Server Components

_Requires `@sentry/nextjs` version `8.28.0` or higher and Next.js 15._
Expand Down
49 changes: 49 additions & 0 deletions platform-includes/capture-error/javascript.nextjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,52 @@ try {
Sentry.captureException(err);
}
```

### Capturing Errors in `error.js` Files

Note that if you create [Next.js error.js files](https://nextjs.org/docs/app/building-your-application/routing/error-handling), these files will take precedence over any global error handling.
This means, that if you want to report errors that are caught by `error.js` files, you need to manually capture them:

```tsx {filename:error.tsx}
"use client";

import { useEffect } from "react";
import * as Sentry from "@sentry/nextjs";

export default function ErrorPage({
error,
}: {
error: Error & { digest?: string };
}) {
useEffect(() => {
// Log the error to Sentry
Sentry.captureException(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
</div>
);
}
```

```jsx {filename:error.jsx}
"use client";

import { useEffect } from "react";
import * as Sentry from "@sentry/nextjs";

export default function ErrorPage({ error }) {
useEffect(() => {
// Log the error to Sentry
Sentry.captureException(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
</div>
);
}
```

0 comments on commit f1bb1dd

Please sign in to comment.