Skip to content

fix(nextjs): Don't capture devmode server-action redirect errors #15485

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 4 commits into from
Feb 25, 2025
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
@@ -1,6 +1,6 @@
import * as Sentry from '@sentry/nextjs';
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { notFound, redirect } from 'next/navigation';

export default function ServerComponent() {
async function myServerAction(formData: FormData) {
Expand All @@ -26,6 +26,17 @@ export default function ServerComponent() {
);
}

async function redirectServerAction(formData: FormData) {
'use server';
return await Sentry.withServerActionInstrumentation(
'redirectServerAction',
{ formData, headers: headers(), recordResponse: true },
() => {
redirect('/');
},
);
}

return (
<>
{/* @ts-ignore */}
Expand All @@ -38,6 +49,11 @@ export default function ServerComponent() {
<input type="text" defaultValue={'some-default-value'} name="some-text-value" />
<button type="submit">Run NotFound Action</button>
</form>
{/* @ts-ignore */}
<form action={redirectServerAction}>
<input type="text" defaultValue={'some-default-value'} name="some-text-value" />
<button type="submit">Run Redirect Action</button>
</form>
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';

const packageJson = require('../package.json');

Expand Down Expand Up @@ -108,19 +108,49 @@ test('Should set not_found status for server actions calling notFound()', async
const nextjsMajor = Number(nextjsVersion.split('.')[0]);
test.skip(!isNaN(nextjsMajor) && nextjsMajor < 14, 'only applies to nextjs apps >= version 14');

const serverComponentTransactionPromise = waitForTransaction('nextjs-app-dir', async transactionEvent => {
const serverActionTransactionPromise = waitForTransaction('nextjs-app-dir', async transactionEvent => {
return transactionEvent?.transaction === 'serverAction/notFoundServerAction';
});

await page.goto('/server-action');
await page.getByText('Run NotFound Action').click();

const transactionEvent = await serverComponentTransactionPromise;
const transactionEvent = await serverActionTransactionPromise;

expect(transactionEvent).toBeDefined();
expect(transactionEvent.contexts?.trace?.status).toBe('not_found');
});

test('Should not capture "NEXT_REDIRECT" control-flow errors for server actions calling redirect()', async ({
page,
}) => {
const nextjsVersion = packageJson.dependencies.next;
const nextjsMajor = Number(nextjsVersion.split('.')[0]);
test.skip(!isNaN(nextjsMajor) && nextjsMajor < 14, 'only applies to nextjs apps >= version 14');

const serverActionTransactionPromise = waitForTransaction('nextjs-app-dir', transactionEvent => {
return transactionEvent?.transaction === 'serverAction/redirectServerAction';
});

let controlFlowErrorCaptured = false;
waitForError('nextjs-app-dir', errorEvent => {
if (errorEvent.exception?.values?.[0].value === 'NEXT_REDIRECT') {
controlFlowErrorCaptured = true;
}

return false;
});

await page.goto('/server-action');
await page.getByText('Run Redirect Action').click();

const serverActionTransactionEvent = await serverActionTransactionPromise;
expect(serverActionTransactionEvent).toBeDefined();

// By the time the server action span finishes the error should already have been sent
expect(controlFlowErrorCaptured).toBe(false);
});

test('Will not include spans in pageload transaction with faulty timestamps for slow loading pages', async ({
page,
}) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export function init(options: BrowserOptions): Client | undefined {
addEventProcessor(filterIncompleteNavigationTransactions);

const filterNextRedirectError: EventProcessor = (event, hint) =>
isRedirectNavigationError(hint?.originalException) ? null : event;
isRedirectNavigationError(hint?.originalException) || event.exception?.values?.[0]?.value === 'NEXT_REDIRECT'
? null
: event;
filterNextRedirectError.id = 'NextRedirectErrorFilter';
addEventProcessor(filterNextRedirectError);

Expand Down
Loading