Skip to content

feat(nextjs): Mark errors caught from NextJS wrappers as unhandled #8893

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

Merged
merged 4 commits into from
Sep 4, 2023
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
2 changes: 1 addition & 1 deletion packages/nextjs/src/common/_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function captureUnderscoreErrorException(contextOrProps: ContextOrP
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
type: 'instrument',
handled: true,
handled: false,
data: {
function: '_error.getInitialProps',
},
Expand Down
26 changes: 23 additions & 3 deletions packages/nextjs/src/common/utils/wrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
startTransaction,
} from '@sentry/core';
import type { Span, Transaction } from '@sentry/types';
import { isString, tracingContextFromHeaders } from '@sentry/utils';
import { addExceptionMechanism, isString, tracingContextFromHeaders } from '@sentry/utils';
import type { IncomingMessage, ServerResponse } from 'http';

import { platformSupportsStreaming } from './platformSupportsStreaming';
Expand Down Expand Up @@ -47,7 +47,17 @@ export function withErrorInstrumentation<F extends (...args: any[]) => any>(
return await origFunction.apply(this, origFunctionArguments);
} catch (e) {
// TODO: Extract error logic from `withSentry` in here or create a new wrapper with said logic or something like that.
captureException(e);
captureException(e, scope => {
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
handled: false,
});
return event;
});

return scope;
});

throw e;
}
};
Expand Down Expand Up @@ -217,7 +227,17 @@ export async function callDataFetcherTraced<F extends (...args: any[]) => Promis
span.finish();

// TODO Copy more robust error handling over from `withSentry`
captureException(err);
captureException(err, scope => {
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
handled: false,
});
return event;
});

return scope;
});

throw err;
}
}
2 changes: 1 addition & 1 deletion packages/nextjs/src/common/wrapApiHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
currentScope.addEventProcessor(event => {
addExceptionMechanism(event, {
type: 'instrument',
handled: true,
handled: false,
data: {
wrapped_handler: wrappingTarget.name,
function: 'withSentry',
Expand Down
14 changes: 12 additions & 2 deletions packages/nextjs/src/common/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
runWithAsyncContext,
startTransaction,
} from '@sentry/core';
import { tracingContextFromHeaders } from '@sentry/utils';
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';

import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
import type { ServerComponentContext } from '../common/types';
Expand Down Expand Up @@ -62,7 +62,17 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
// We don't want to report redirects
} else {
transaction.setStatus('internal_error');
captureException(e);

captureException(e, scope => {
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
handled: false,
});
return event;
});

return scope;
});
}

transaction.finish();
Expand Down