Skip to content

Commit 0d3f1b1

Browse files
authored
fix(nextjs): Mark redirect server actions as ok instead of internal_error (#21521)
Server Actions that call `redirect()` inside `withServerActionInstrumentation` were recorded with span status `internal_error` instead of `ok`. We now explicitly set it to `ok` and end the span in a redirect case.
1 parent 1bf2bad commit 0d3f1b1

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ test('Should not capture "NEXT_REDIRECT" control-flow errors for server actions
149149
const serverActionTransactionEvent = await serverActionTransactionPromise;
150150
expect(serverActionTransactionEvent).toBeDefined();
151151

152+
// Redirects are normal control flow, so the transaction must not be flagged as errored
153+
expect(serverActionTransactionEvent.contexts?.trace?.status).toBe('ok');
154+
152155
// By the time the server action span finishes the error should already have been sent
153156
expect(controlFlowErrorCaptured).toBe(false);
154157
});

‎packages/nextjs/src/common/withServerActionInstrumentation.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1111
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1212
SPAN_STATUS_ERROR,
13+
SPAN_STATUS_OK,
1314
startSpan,
1415
withIsolationScope,
1516
} from '@sentry/core';
@@ -126,7 +127,11 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
126127
// We don't want to report "not-found"s
127128
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
128129
} else if (isRedirectNavigationError(error)) {
129-
// Don't do anything for redirects
130+
// Redirects are normal Next.js control flow, not errors. Mark the span as OK and end it
131+
// early so the surrounding `startSpan` error handler doesn't override the status to
132+
// `internal_error`
133+
span.setStatus({ code: SPAN_STATUS_OK });
134+
span.end();
130135
} else {
131136
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
132137
captureException(error, {

0 commit comments

Comments
 (0)