Skip to content

Commit

Permalink
sentry: fix up import in _error
Browse files Browse the repository at this point in the history
  • Loading branch information
mimecuvalo committed Nov 3, 2023
1 parent c1e5208 commit 6342152
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
// @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<AppErrorProps> = ({ 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
}

return <NextErrorComponent statusCode={statusCode} />;
};

MyError.getInitialProps = async (context) => {
const errorInitialProps = await NextErrorComponent.getInitialProps(context);
const errorInitialProps: AppErrorProps = await NextErrorComponent.getInitialProps(context);

const { res, err, asPath } = context;

Expand All @@ -43,20 +49,20 @@ 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;
}

// 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;
};
Expand Down

0 comments on commit 6342152

Please sign in to comment.