sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

`NEXT_REDIRECT` error to be reported as an Unhandled Error in Server Action redirect function.

Open NEKOYASAN opened this issue 2 years ago • 6 comments

Is there an existing issue for this?

  • [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [x] I have reviewed the documentation https://docs.sentry.io/
  • [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.40.0

Framework Version

React 19.0.0-rc-66855b96-20241106 & Next.js 15.0.3

Link to Sentry event

https://nekoyasan.sentry.io/issues/6082760807/events/a7a626e05026406bb3d13a68e8270c92/

Reproduction Example/SDK Setup

Reproduction Example: https://github.com/NEKOYASAN/next-sentry-redirect-minimum-repro

Reproduction Steps (From completely empty)

  1. pnpm create next-app
  2. npx @sentry/wizard@latest -i nextjs --saas
  3. add Server Action & Components (redirect function in Server Action)

Steps to Reproduce

  1. pnpm run build
  2. pnpm run start
  3. Click Redirect to example page Button
  4. Redirects normally to /example (but NEXT_REDIRECT Error is reported to Sentry)

Expected Result

Maybe NEXT_REDIRECT should not be reported to Sentry.

Actual Result

Server Action reports NEXT_REDIRECT as Unhandled Error.

but it does not seem to be reported in page.tsx, so there seems to be no problem.

NEKOYASAN avatar Nov 22 '24 14:11 NEKOYASAN

Maybe ref: #7641 and #10038

NEKOYASAN avatar Nov 22 '24 19:11 NEKOYASAN

Maybe ref: #7641 and #10038

Definitely ref, but funny enough it is still happening. +1

Zinnavoy avatar Nov 23 '24 06:11 Zinnavoy

One way to prevent reporting is to add beforeSend to sentry.client.config.ts


Sentry.init({
  /* etc... */
  beforeSend(event, hint) {
    const error = hint.originalException as Error & { digest?: unknown };
    if (
      error &&
      error.digest &&
      typeof error.digest === 'string' &&
      error.digest.startsWith('NEXT_REDIRECT;')
    ) {
      return null;
    }
    return event;
  },
  /* etc... */
});

NEKOYASAN avatar Nov 24 '24 00:11 NEKOYASAN

NEXT_REDIRECT should have been absorbed by the client-side redirect error boundary, but was somehow reported to Sentry as an Unhandled Error.

I don't remember seeing it on Next.js 15-rc0 + sentry/nextjs 8.27.0 , so maybe something changed between Next.js rc1,2 ~ 15.0.3

NEKOYASAN avatar Nov 24 '24 00:11 NEKOYASAN

One way to prevent reporting is to add beforeSend to sentry.client.config.ts

Sentry.init({ /* etc... / beforeSend(event, hint) { const error = hint.originalException as Error & { digest?: unknown }; if ( error && error.digest && typeof error.digest === 'string' && error.digest.startsWith('NEXT_REDIRECT;') ) { return null; } return event; }, / etc... */ });

This did the trick for me, thank you.

Zinnavoy avatar Nov 25 '24 23:11 Zinnavoy

A PR closing this issue has just been released 🚀

This issue was referenced by PR #14440, which was included in the 8.41.0 release.

github-actions[bot] avatar Nov 27 '24 12:11 github-actions[bot]