Skip to content

Commit 003a737

Browse files
authored
feat(nextjs): Use helper function in _error.js example (getsentry#5397)
This updates the nextjs manual setup docs to use the helper function for `_error.js` introduced in getsentry/sentry-javascript#5259.
1 parent 3c12ca0 commit 003a737

File tree

1 file changed

+28
-53
lines changed

1 file changed

+28
-53
lines changed

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

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -79,67 +79,42 @@ In serverless deployment environments, including Vercel, the Next.js server runs
7979
To capture these errors in Sentry, you can use the Next.js [error page customization](https://nextjs.org/docs/advanced-features/custom-error-page#reusing-the-built-in-error-page) option. To do this, create `pages/_error.js`, and include the following:
8080

8181
```javascript {filename:pages/_error.js}
82-
import NextErrorComponent from "next/error";
82+
/**
83+
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.
84+
*
85+
* This page is loaded by Nextjs:
86+
* - on the server, when data-fetching methods throw or reject
87+
* - on the client, when `getInitialProps` throws or rejects
88+
* - on the client, when a React lifecycle method throws or rejects, and it's
89+
* caught by the built-in Nextjs error boundary
90+
*
91+
* See:
92+
* - https://nextjs.org/docs/basic-features/data-fetching/overview
93+
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
94+
* - https://reactjs.org/docs/error-boundaries.html
95+
*/
8396

8497
import * as Sentry from "@sentry/nextjs";
98+
import NextErrorComponent from "next/error";
8599

86-
const MyError = ({ statusCode, hasGetInitialPropsRun, err }) => {
87-
if (!hasGetInitialPropsRun && err) {
88-
// getInitialProps is not called in case of
89-
// https://github.com/vercel/next.js/issues/8592. As a workaround, we pass
90-
// err via _app.js so it can be captured
91-
Sentry.captureException(err);
92-
// Flushing is not required in this case as it only happens on the client
93-
}
100+
const CustomErrorComponent = props => {
101+
// If you're using a Nextjs version prior to 12.2.1, uncomment this to
102+
// compensate for https://github.com/vercel/next.js/issues/8592
103+
// Sentry.captureUnderscoreErrorException(props);
94104

95-
return <NextErrorComponent statusCode={statusCode} />;
105+
return <NextErrorComponent statusCode={props.statusCode} />;
96106
};
97107

98-
MyError.getInitialProps = async ({ res, err, asPath }) => {
99-
const errorInitialProps = await NextErrorComponent.getInitialProps({
100-
res,
101-
err,
102-
});
103-
104-
// Workaround for https://github.com/vercel/next.js/issues/8592, mark when
105-
// getInitialProps has run
106-
errorInitialProps.hasGetInitialPropsRun = true;
107-
108-
// Running on the server, the response object (`res`) is available.
109-
//
110-
// Next.js will pass an err on the server if a page's data fetching methods
111-
// threw or returned a Promise that rejected
112-
//
113-
// Running on the client (browser), Next.js will provide an err if:
114-
//
115-
// - a page's `getInitialProps` threw or returned a Promise that rejected
116-
// - an exception was thrown somewhere in the React lifecycle (render,
117-
// componentDidMount, etc) that was caught by Next.js's React Error
118-
// Boundary. Read more about what types of exceptions are caught by Error
119-
// Boundaries: https://reactjs.org/docs/error-boundaries.html
120-
121-
if (err) {
122-
Sentry.captureException(err);
123-
124-
// Flushing before returning is necessary if deploying to Vercel, see
125-
// https://vercel.com/docs/platform/limits#streaming-responses
126-
await Sentry.flush(2000);
127-
128-
return errorInitialProps;
129-
}
130-
131-
// If this point is reached, getInitialProps was called without any
132-
// information about what the error might be. This is unexpected and may
133-
// indicate a bug introduced in Next.js, so record it in Sentry
134-
Sentry.captureException(
135-
new Error(`_error.js getInitialProps missing data at path: ${asPath}`)
136-
);
137-
await Sentry.flush(2000);
138-
139-
return errorInitialProps;
108+
CustomErrorComponent.getInitialProps = async contextData => {
109+
// In case this is running in a serverless function, await this in order to give Sentry
110+
// time to send the error before the lambda exits
111+
await Sentry.captureUnderscoreErrorException(contextData);
112+
113+
// This will contain the status code of the response
114+
return NextErrorComponent.getInitialProps(contextData);
140115
};
141116

142-
export default MyError;
117+
export default CustomErrorComponent;
143118
```
144119

145120
## Extend Next.js Configuration

0 commit comments

Comments
 (0)