Skip to content

fix(nextjs): Don't capture not-found and redirect errors in generation functions #10057

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 3 commits into from
Jan 5, 2024
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
@@ -0,0 +1,11 @@
import { notFound } from 'next/navigation';

export const dynamic = 'force-dynamic';

export default function PageWithRedirect() {
return <p>Hello World!</p>;
}

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

export const dynamic = 'force-dynamic';

export default function PageWithRedirect() {
return <p>Hello World!</p>;
}

export async function generateMetadata() {
redirect('/');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>Home</p>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,37 @@ test('Should send a transaction and an error event for a faulty generateViewport
expect(await transactionPromise).toBeDefined();
expect(await errorEventPromise).toBeDefined();
});

test('Should send a transaction event with correct status for a generateMetadata() function invokation with redirect()', async ({
page,
}) => {
const testTitle = 'redirect-foobar';

const transactionPromise = waitForTransaction('nextjs-14', async transactionEvent => {
return (
transactionEvent?.transaction === 'Page.generateMetadata (/generation-functions/with-redirect)' &&
transactionEvent.contexts?.trace?.data?.['searchParams']?.['metadataTitle'] === testTitle
);
});

await page.goto(`/generation-functions/with-redirect?metadataTitle=${testTitle}`);

expect((await transactionPromise).contexts?.trace?.status).toBe('ok');
});

test('Should send a transaction event with correct status for a generateMetadata() function invokation with notfound()', async ({
page,
}) => {
const testTitle = 'notfound-foobar';

const transactionPromise = waitForTransaction('nextjs-14', async transactionEvent => {
return (
transactionEvent?.transaction === 'Page.generateMetadata (/generation-functions/with-notfound)' &&
transactionEvent.contexts?.trace?.data?.['searchParams']?.['metadataTitle'] === testTitle
);
});

await page.goto(`/generation-functions/with-notfound?metadataTitle=${testTitle}`);

expect((await transactionPromise).contexts?.trace?.status).toBe('not_found');
});
36 changes: 25 additions & 11 deletions packages/nextjs/src/common/wrapGenerationFunctionWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
getCurrentScope,
handleCallbackErrors,
runWithAsyncContext,
startSpan,
startSpanManual,
} from '@sentry/core';
import type { WebFetchHeaders } from '@sentry/types';
import { winterCGHeadersToDict } from '@sentry/utils';

import type { GenerationFunctionContext } from '../common/types';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
import { commonObjectToPropagationContext } from './utils/commonObjectTracing';

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
transactionContext.parentSpanId = commonSpanId;
}

return startSpan(
return startSpanManual(
{
op: 'function.nextjs',
name: `${componentType}.${generationFunctionIdentifier} (${componentRoute})`,
Expand All @@ -76,18 +77,31 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
},
},
},
() => {
span => {
return handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
err =>
captureException(err, {
mechanism: {
handled: false,
data: {
function: 'wrapGenerationFunctionWithSentry',
err => {
if (isNotFoundNavigationError(err)) {
// We don't want to report "not-found"s
span?.setStatus('not_found');
} else if (isRedirectNavigationError(err)) {
// We don't want to report redirects
span?.setStatus('ok');
} else {
span?.setStatus('internal_error');
captureException(err, {
mechanism: {
handled: false,
data: {
function: 'wrapGenerationFunctionWithSentry',
},
},
},
}),
});
}
},
() => {
span?.end();
},
);
},
);
Expand Down