Skip to content

Commit

Permalink
feat(nextjs): Mark errors caught from NextJS wrappers as unhandled (#…
Browse files Browse the repository at this point in the history
…8893)

Mark errors caught in

* `captureUnderscoreException` 
* `wrapApiHandlerWithSentry`
* `callDataFetcherTraced`
* `withErrorInstrumentation`
* `wrapServerComponentWithSentry`

as unhandled.

For more details, see #8890, #6073

Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
  • Loading branch information
Lms24 and lforst authored Sep 4, 2023
1 parent 3d8b444 commit b102817
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
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 @@ -221,7 +231,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 @@ -191,7 +191,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 @@ -63,7 +63,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

0 comments on commit b102817

Please sign in to comment.