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