Skip to content
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