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 report NEXT_NOT_FOUND and NEXT_REDIRECT errors #7642

Merged
merged 5 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/nextjs/src/common/nextNavigationErrorUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isError } from '@sentry/utils';

/**
* Determines whether input is a Next.js not-found error.
* https://beta.nextjs.org/docs/api-reference/notfound#notfound
*/
export function isNotFoundNavigationError(subject: unknown): boolean {
return isError(subject) && (subject as Error & { digest?: unknown }).digest === 'NEXT_NOT_FOUND';
}

/**
* Determines whether input is a Next.js redirect error.
* https://beta.nextjs.org/docs/api-reference/redirect#redirect
*/
export function isRedirectNavigationError(subject: unknown): boolean {
return (
isError(subject) &&
typeof (subject as Error & { digest?: unknown }).digest === 'string' &&
(subject as Error & { digest: string }).digest.startsWith('NEXT_REDIRECT;') // a redirect digest looks like "NEXT_REDIRECT;[redirect path]"
);
}
25 changes: 18 additions & 7 deletions packages/nextjs/src/server/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { addTracingExtensions, captureException, getCurrentHub, startTransaction
import { baggageHeaderToDynamicSamplingContext, extractTraceparentData } from '@sentry/utils';
import * as domain from 'domain';

import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
import type { ServerComponentContext } from '../common/types';

/**
Expand Down Expand Up @@ -45,12 +46,24 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
currentScope.setSpan(transaction);
}

const handleErrorCase = (e: unknown): void => {
if (isNotFoundNavigationError(e)) {
// We don't want to report "not-found"s
transaction.setStatus('not_found');
} else if (isRedirectNavigationError(e)) {
// We don't want to report redirects
} else {
transaction.setStatus('internal_error');
captureException(e);
}

transaction.finish();
};

try {
maybePromiseResult = originalFunction.apply(thisArg, args);
} catch (e) {
transaction.setStatus('internal_error');
captureException(e);
transaction.finish();
handleErrorCase(e);
throw e;
}

Expand All @@ -60,10 +73,8 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
() => {
transaction.finish();
},
(e: Error) => {
transaction.setStatus('internal_error');
captureException(e);
transaction.finish();
e => {
handleErrorCase(e);
},
);

Expand Down