|
| 1 | +import NextErrorComponent, { ErrorProps } from 'next/error'; |
| 2 | +import { NextPageContext } from 'next'; |
| 3 | + |
| 4 | +import * as Sentry from '@sentry/nextjs'; |
| 5 | + |
| 6 | +interface PageProps { |
| 7 | + hasGetInitialPropsRun?: boolean; |
| 8 | + err?: NextPageContext['err']; |
| 9 | +} |
| 10 | + |
| 11 | +const MyError = ({ |
| 12 | + statusCode, |
| 13 | + hasGetInitialPropsRun, |
| 14 | + err, |
| 15 | +}: ErrorProps & PageProps) => { |
| 16 | + if (!hasGetInitialPropsRun && err) { |
| 17 | + // getInitialProps is not called in case of |
| 18 | + // https://github.com/vercel/next.js/issues/8592. As a workaround, we pass |
| 19 | + // err via _app.js so it can be captured |
| 20 | + Sentry.captureException(err); |
| 21 | + // Flushing is not required in this case as it only happens on the client |
| 22 | + } |
| 23 | + |
| 24 | + return <NextErrorComponent statusCode={statusCode} />; |
| 25 | +}; |
| 26 | + |
| 27 | +MyError.getInitialProps = async (context: NextPageContext) => { |
| 28 | + const errorInitialProps = await NextErrorComponent.getInitialProps(context); |
| 29 | + |
| 30 | + const { res, err, asPath } = context; |
| 31 | + |
| 32 | + // Workaround for https://github.com/vercel/next.js/issues/8592, mark when |
| 33 | + // getInitialProps has run |
| 34 | + const errorProps: ErrorProps & PageProps = { |
| 35 | + ...errorInitialProps, |
| 36 | + hasGetInitialPropsRun: true, |
| 37 | + }; |
| 38 | + |
| 39 | + // Returning early because we don't want to log 404 errors to Sentry. |
| 40 | + if (res?.statusCode === 404) { |
| 41 | + return errorProps; |
| 42 | + } |
| 43 | + |
| 44 | + // Running on the server, the response object (`res`) is available. |
| 45 | + // |
| 46 | + // Next.js will pass an err on the server if a page's data fetching methods |
| 47 | + // threw or returned a Promise that rejected |
| 48 | + // |
| 49 | + // Running on the client (browser), Next.js will provide an err if: |
| 50 | + // |
| 51 | + // - a page's `getInitialProps` threw or returned a Promise that rejected |
| 52 | + // - an exception was thrown somewhere in the React lifecycle (render, |
| 53 | + // componentDidMount, etc) that was caught by Next.js's React Error |
| 54 | + // Boundary. Read more about what types of exceptions are caught by Error |
| 55 | + // Boundaries: https://reactjs.org/docs/error-boundaries.html |
| 56 | + |
| 57 | + if (err) { |
| 58 | + Sentry.captureException(err); |
| 59 | + |
| 60 | + // Flushing before returning is necessary if deploying to Vercel, see |
| 61 | + // https://vercel.com/docs/platform/limits#streaming-responses |
| 62 | + await Sentry.flush(2000); |
| 63 | + |
| 64 | + return errorProps; |
| 65 | + } |
| 66 | + |
| 67 | + // If this point is reached, getInitialProps was called without any |
| 68 | + // information about what the error might be. This is unexpected and may |
| 69 | + // indicate a bug introduced in Next.js, so record it in Sentry |
| 70 | + Sentry.captureException( |
| 71 | + new Error(`_error.js getInitialProps missing data at path: ${asPath}`) |
| 72 | + ); |
| 73 | + await Sentry.flush(2000); |
| 74 | + |
| 75 | + return errorProps; |
| 76 | +}; |
| 77 | + |
| 78 | +export default MyError; |
0 commit comments