Skip to content

fix(nextjs): Do not report redirects and notFound calls as errors in server actions #10474

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 1 commit into from
Feb 2, 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
@@ -1,5 +1,6 @@
import * as Sentry from '@sentry/nextjs';
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';

export default function ServerComponent() {
async function myServerAction(formData: FormData) {
Expand All @@ -14,11 +15,29 @@ export default function ServerComponent() {
);
}

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

return (
// @ts-ignore
<form action={myServerAction}>
<input type="text" defaultValue={'some-default-value'} name="some-text-value" />
<button type="submit">Run Action</button>
</form>
<>
{/* @ts-ignore */}
<form action={myServerAction}>
<input type="text" defaultValue={'some-default-value'} name="some-text-value" />
<button type="submit">Run Action</button>
</form>
{/* @ts-ignore */}
<form action={notFoundServerAction}>
<input type="text" defaultValue={'some-default-value'} name="some-text-value" />
<button type="submit">Run NotFound Action</button>
</form>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ test('Should send a transaction for instrumented server actions', async ({ page
expect(Object.keys((await serverComponentTransactionPromise).request?.headers || {}).length).toBeGreaterThan(0);
});

test('Should set not_found status for server actions calling notFound()', 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 serverComponentTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => {
return transactionEvent?.transaction === 'serverAction/notFoundServerAction';
});

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

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

test('Will not include spans in pageload transaction with faulty timestamps for slow loading pages', async ({
page,
}) => {
Expand Down
15 changes: 14 additions & 1 deletion packages/nextjs/src/common/withServerActionInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { logger, tracingContextFromHeaders } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
import { platformSupportsStreaming } from './utils/platformSupportsStreaming';
import { flushQueue } from './utils/responseEnd';

Expand Down Expand Up @@ -101,7 +102,19 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
},
async span => {
const result = await handleCallbackErrors(callback, error => {
captureException(error, { mechanism: { handled: false } });
if (isNotFoundNavigationError(error)) {
// We don't want to report "not-found"s
span?.setStatus('not_found');
} else if (isRedirectNavigationError(error)) {
// Don't do anything for redirects
} else {
span?.setStatus('internal_error');
captureException(error, {
mechanism: {
handled: false,
},
});
}
});

if (options.recordResponse !== undefined ? options.recordResponse : sendDefaultPii) {
Expand Down