Last active
November 30, 2023 04:35
-
-
Save Froelund/4d04d94931fa81a245a7dba8fc90e68a to your computer and use it in GitHub Desktop.
Next.js Typescript error reporting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { captureException, flush } from '@sentry/nextjs'; | |
import NextErrorComponent from 'next/error'; | |
import type { ErrorProps } from 'next/error'; | |
import type { NextPage } from 'next'; | |
interface AppErrorProps extends ErrorProps { | |
err?: Error; | |
hasGetInitialPropsRun?: boolean; | |
} | |
const AppError: NextPage<AppErrorProps> = ({ | |
hasGetInitialPropsRun, | |
err, | |
statusCode, | |
}) => { | |
if (!hasGetInitialPropsRun && err) { | |
captureException(err); | |
} | |
return <NextErrorComponent statusCode={statusCode} />; | |
}; | |
AppError.getInitialProps = async (ctx) => { | |
const errorInitialProps: AppErrorProps = | |
await NextErrorComponent.getInitialProps(ctx); | |
errorInitialProps.hasGetInitialPropsRun = true; | |
if (ctx.err) { | |
captureException(ctx.err); | |
await flush(2000); | |
return errorInitialProps; | |
} | |
captureException( | |
new Error(`_error.tsx getInitialProps missing data at path: ${ctx.asPath}`) | |
); | |
await flush(2000); | |
return errorInitialProps; | |
}; | |
export default AppError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot🙏