Skip to content

fix(nextjs): Don't report NEXT_NOT_FOUND and NEXT_REDIRECT errors #7642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export default function Layout({ children }: { children: React.ReactNode }) {
<li>
<Link href="/server-component/parameter/foo/bar/baz">/server-component/parameter/foo/bar/baz</Link>
</li>
<li>
<Link href="/not-found">/not-found</Link>
</li>
<li>
<Link href="/redirect">/redirect</Link>
</li>
</ul>
<TransactionContextProvider>{children}</TransactionContextProvider>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ClientErrorDebugTools } from '../components/client-error-debug-tools';

export default function NotFound() {
return (
<div style={{ border: '1px solid lightgrey', padding: '12px' }}>
<h2>Not found (/)</h2>;
<ClientErrorDebugTools />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { notFound } from 'next/navigation';

export const dynamic = 'force-dynamic';

export default function Page() {
notFound();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { redirect } from 'next/navigation';

export const dynamic = 'force-dynamic';

export default function Page() {
redirect('/');
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,26 @@ if (process.env.TEST_ENV === 'production') {
)
.toBe(200);
});

test('Should not set an error status on a server component transaction when it redirects', async ({ page }) => {
const serverComponentTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => {
return transactionEvent?.transaction === 'Page Server Component (/server-component/redirect)';
});

await page.goto('/server-component/redirect');

expect((await serverComponentTransactionPromise).contexts?.trace?.status).not.toBe('internal_error');
});

test('Should set a "not_found" status on a server component transaction when notFound() is called', async ({
page,
}) => {
const serverComponentTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => {
return transactionEvent?.transaction === 'Page Server Component (/server-component/not-found)';
});

await page.goto('/server-component/not-found');

expect((await serverComponentTransactionPromise).contexts?.trace?.status).toBe('not_found');
});
}
21 changes: 21 additions & 0 deletions packages/nextjs/src/common/nextNavigationErrorUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isError } from '@sentry/utils';

/**
* Determines whether input is a Next.js not-found error.
* https://beta.nextjs.org/docs/api-reference/notfound#notfound
*/
export function isNotFoundNavigationError(subject: unknown): boolean {
return isError(subject) && (subject as Error & { digest?: unknown }).digest === 'NEXT_NOT_FOUND';
}

/**
* Determines whether input is a Next.js redirect error.
* https://beta.nextjs.org/docs/api-reference/redirect#redirect
*/
export function isRedirectNavigationError(subject: unknown): boolean {
return (
isError(subject) &&
typeof (subject as Error & { digest?: unknown }).digest === 'string' &&
(subject as Error & { digest: string }).digest.startsWith('NEXT_REDIRECT;') // a redirect digest looks like "NEXT_REDIRECT;[redirect path]"
);
}
25 changes: 18 additions & 7 deletions packages/nextjs/src/server/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { addTracingExtensions, captureException, getCurrentHub, startTransaction
import { baggageHeaderToDynamicSamplingContext, extractTraceparentData } from '@sentry/utils';
import * as domain from 'domain';

import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
import type { ServerComponentContext } from '../common/types';

/**
Expand Down Expand Up @@ -45,12 +46,24 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
currentScope.setSpan(transaction);
}

const handleErrorCase = (e: unknown): void => {
if (isNotFoundNavigationError(e)) {
// We don't want to report "not-found"s
transaction.setStatus('not_found');
} else if (isRedirectNavigationError(e)) {
// We don't want to report redirects
} else {
transaction.setStatus('internal_error');
captureException(e);
}

transaction.finish();
};

try {
maybePromiseResult = originalFunction.apply(thisArg, args);
} catch (e) {
transaction.setStatus('internal_error');
captureException(e);
transaction.finish();
handleErrorCase(e);
throw e;
}

Expand All @@ -60,10 +73,8 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
() => {
transaction.finish();
},
(e: Error) => {
transaction.setStatus('internal_error');
captureException(e);
transaction.finish();
e => {
handleErrorCase(e);
},
);

Expand Down