Skip to content

Commit 519d22b

Browse files
lforstlobsterkatie
andauthored
Add typescript version of Next.js _error.js (#5414)
Co-authored-by: Katie Byers <[email protected]>
1 parent d91feab commit 519d22b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/platforms/javascript/guides/nextjs/manual-setup.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ CustomErrorComponent.getInitialProps = async contextData => {
117117
export default CustomErrorComponent;
118118
```
119119

120+
```typescript {filename:pages/_error.ts}
121+
/**
122+
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.
123+
*
124+
* This page is loaded by Nextjs:
125+
* - on the server, when data-fetching methods throw or reject
126+
* - on the client, when `getInitialProps` throws or rejects
127+
* - on the client, when a React lifecycle method throws or rejects, and it's
128+
* caught by the built-in Nextjs error boundary
129+
*
130+
* See:
131+
* - https://nextjs.org/docs/basic-features/data-fetching/overview
132+
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
133+
* - https://reactjs.org/docs/error-boundaries.html
134+
*/
135+
136+
import * as Sentry from "@sentry/nextjs";
137+
import type { NextPage } from "next";
138+
import type { ErrorProps } from "next/error";
139+
import NextErrorComponent from "next/error";
140+
141+
const CustomErrorComponent: NextPage<ErrorProps> = props => {
142+
// If you're using a Nextjs version prior to 12.2.1, uncomment this to
143+
// compensate for https://github.com/vercel/next.js/issues/8592
144+
// Sentry.captureUnderscoreErrorException(props);
145+
146+
return <NextErrorComponent statusCode={props.statusCode} />;
147+
};
148+
149+
CustomErrorComponent.getInitialProps = async contextData => {
150+
// In case this is running in a serverless function, await this in order to give Sentry
151+
// time to send the error before the lambda exits
152+
await Sentry.captureUnderscoreErrorException(contextData);
153+
154+
// This will contain the status code of the response
155+
return NextErrorComponent.getInitialProps(contextData);
156+
};
157+
158+
export default CustomErrorComponent;
159+
```
160+
120161
## Extend Next.js Configuration
121162

122163
Use `withSentryConfig` to extend the default Next.js usage of Webpack. This will do two things:

0 commit comments

Comments
 (0)