From 62e562eea68f33f3738d285c84477f77399fbe00 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 8 Nov 2023 15:30:46 +0100 Subject: [PATCH] fix(nextjs): Flush servercomponent events for edge (#9487) --- .../app/edge-server-components/error/page.tsx | 7 +++++++ .../nextjs-app-dir/tests/edge.test.ts | 12 ++++++++++++ .../src/common/wrapServerComponentWithSentry.ts | 4 ++++ 3 files changed, 23 insertions(+) create mode 100644 packages/e2e-tests/test-applications/nextjs-app-dir/app/edge-server-components/error/page.tsx create mode 100644 packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge.test.ts diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/app/edge-server-components/error/page.tsx b/packages/e2e-tests/test-applications/nextjs-app-dir/app/edge-server-components/error/page.tsx new file mode 100644 index 000000000000..35d15616fd1c --- /dev/null +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/app/edge-server-components/error/page.tsx @@ -0,0 +1,7 @@ +export const dynamic = 'force-dynamic'; + +export const runtime = 'edge'; + +export default async function Page() { + throw new Error('Edge Server Component Error'); +} diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge.test.ts b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge.test.ts new file mode 100644 index 000000000000..26a9e7fed310 --- /dev/null +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge.test.ts @@ -0,0 +1,12 @@ +import { test, expect } from '@playwright/test'; +import { waitForError } from '../event-proxy-server'; + +test('Should record exceptions for faulty edge server components', async ({ page }) => { + const errorEventPromise = waitForError('nextjs-13-app-dir', errorEvent => { + return errorEvent?.exception?.values?.[0]?.value === 'Edge Server Component Error'; + }); + + await page.goto('/edge-server-components/error'); + + expect(await errorEventPromise).toBeDefined(); +}); diff --git a/packages/nextjs/src/common/wrapServerComponentWithSentry.ts b/packages/nextjs/src/common/wrapServerComponentWithSentry.ts index b39ca674af6e..360b169c7b37 100644 --- a/packages/nextjs/src/common/wrapServerComponentWithSentry.ts +++ b/packages/nextjs/src/common/wrapServerComponentWithSentry.ts @@ -1,6 +1,7 @@ import { addTracingExtensions, captureException, + flush, getCurrentHub, runWithAsyncContext, startTransaction, @@ -81,6 +82,7 @@ export function wrapServerComponentWithSentry any> maybePromiseResult = originalFunction.apply(thisArg, args); } catch (e) { handleErrorCase(e); + void flush(); throw e; } @@ -94,12 +96,14 @@ export function wrapServerComponentWithSentry any> handleErrorCase(e); }, ); + void flush(); // It is very important that we return the original promise here, because Next.js attaches various properties // to that promise and will throw if they are not on the returned value. return maybePromiseResult; } else { transaction.finish(); + void flush(); return maybePromiseResult; } });