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

fix(nextjs): Don't capture suspense errors in server components #12261

Merged
merged 5 commits into from
May 28, 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,15 @@
import * as Sentry from '@sentry/nextjs';
import { use } from 'react';
export const dynamic = 'force-dynamic';

export default async function Page() {
try {
use(fetch('http://example.com/'));
} catch (e) {
Sentry.captureException(e); // This error should not be reported
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for any async event processors to run
await Sentry.flush();
}

return <p>test</p>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/event-proxy-server';

test('should not capture serverside suspense errors', async ({ page }) => {
const pageServerComponentTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === 'Page Server Component (/suspense-error)';
});

let errorEvent;
waitForError('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === 'Page Server Component (/suspense-error)';
}).then(event => {
errorEvent = event;
});

await page.goto(`/suspense-error`);

await page.waitForTimeout(5000);

const pageServerComponentTransaction = await pageServerComponentTransactionPromise;
expect(pageServerComponentTransaction).toBeDefined();

expect(errorEvent).toBeUndefined();
});
14 changes: 12 additions & 2 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,23 @@ export function init(options: NodeOptions): void {

const originalException = hint.originalException;

const isReactControlFlowError =
const isPostponeError =
typeof originalException === 'object' &&
originalException !== null &&
'$$typeof' in originalException &&
originalException.$$typeof === Symbol.for('react.postpone');

if (isReactControlFlowError) {
if (isPostponeError) {
// Postpone errors are used for partial-pre-rendering (PPR)
return null;
}

// We don't want to capture suspense errors as they are simply used by React/Next.js for control flow
const exceptionMessage = event.exception?.values?.[0]?.value;
if (
exceptionMessage?.includes('Suspense Exception: This is not a real error!') ||
exceptionMessage?.includes('Suspense Exception: This is not a real error, and should not leak')
) {
return null;
}

Expand Down
Loading