Skip to content

Instantly share code, notes, and snippets.

@brunos3d
Last active July 7, 2022 15:31
Show Gist options
  • Save brunos3d/6bda5b3d2f6220ac664af16b7e9418ee to your computer and use it in GitHub Desktop.
Save brunos3d/6bda5b3d2f6220ac664af16b7e9418ee to your computer and use it in GitHub Desktop.
Next.js with Sentry (_error.tsx file)
import NextErrorComponent, { ErrorProps } from 'next/error';
import { captureException, flush } from '@sentry/nextjs';
import type { NextPage, NextPageContext } from 'next/types';
export interface AppErrorProps extends ErrorProps {
err?: Error;
hasGetInitialPropsRun?: boolean;
}
const AppError: NextPage<AppErrorProps> = ({
statusCode,
hasGetInitialPropsRun,
err,
}) => {
if (!hasGetInitialPropsRun && err) {
captureException(err);
}
return <NextErrorComponent statusCode={statusCode} />;
};
AppError.getInitialProps = async (context: NextPageContext) => {
const errorInitialProps: AppErrorProps =
await NextErrorComponent.getInitialProps(context);
errorInitialProps.hasGetInitialPropsRun = true;
if (context.err) {
captureException(context.err);
await flush(2000);
return errorInitialProps;
}
return errorInitialProps;
};
export default AppError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment