diff --git a/pages/_error.tsx b/pages/_error.tsx index a375727..8bbd89e 100644 --- a/pages/_error.tsx +++ b/pages/_error.tsx @@ -1,14 +1,20 @@ -// @ts-nocheck -import * as Sentry from '@sentry/nextjs'; +import { captureException, flush } from '@sentry/nextjs'; +import type { ErrorProps } from 'next/error'; +import type { NextPage } from 'next'; import NextErrorComponent from 'next/error'; -const MyError = ({ statusCode, hasGetInitialPropsRun, err }) => { +interface AppErrorProps extends ErrorProps { + err?: Error; + hasGetInitialPropsRun?: boolean; +} + +const MyError: NextPage = ({ statusCode, hasGetInitialPropsRun, err }) => { if (!hasGetInitialPropsRun && err) { // getInitialProps is not called in case of // https://github.com/vercel/next.js/issues/8592. As a workaround, we pass // err via _app.js so it can be captured - Sentry.captureException(err); + captureException(err); // Flushing is not required in this case as it only happens on the client } @@ -16,7 +22,7 @@ const MyError = ({ statusCode, hasGetInitialPropsRun, err }) => { }; MyError.getInitialProps = async (context) => { - const errorInitialProps = await NextErrorComponent.getInitialProps(context); + const errorInitialProps: AppErrorProps = await NextErrorComponent.getInitialProps(context); const { res, err, asPath } = context; @@ -43,11 +49,11 @@ MyError.getInitialProps = async (context) => { // Boundaries: https://reactjs.org/docs/error-boundaries.html if (err) { - Sentry.captureException(err); + captureException(err); // Flushing before returning is necessary if deploying to Vercel, see // https://vercel.com/docs/platform/limits#streaming-responses - await Sentry.flush(2000); + await flush(2000); return errorInitialProps; } @@ -55,8 +61,8 @@ MyError.getInitialProps = async (context) => { // If this point is reached, getInitialProps was called without any // information about what the error might be. This is unexpected and may // indicate a bug introduced in Next.js, so record it in Sentry - Sentry.captureException(new Error(`_error.js getInitialProps missing data at path: ${asPath}`)); - await Sentry.flush(2000); + captureException(new Error(`_error.js getInitialProps missing data at path: ${asPath}`)); + await flush(2000); return errorInitialProps; };