Skip to content

Commit a9e26e9

Browse files
committed
review comments
1 parent a0050bf commit a9e26e9

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
addTracingExtensions,
44
captureException,
55
getActiveSpan,
6-
getIsolationScope,
76
getRootSpan,
87
handleCallbackErrors,
98
setHttpStatus,
9+
withIsolationScope,
1010
} from '@sentry/core';
1111
import { winterCGHeadersToDict } from '@sentry/utils';
1212
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
@@ -28,50 +28,52 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
2828

2929
return new Proxy(routeHandler, {
3030
apply: async (originalFunction, thisArg, args) => {
31-
getIsolationScope().setSDKProcessingMetadata({
32-
request: {
33-
headers: headers ? winterCGHeadersToDict(headers) : undefined,
34-
},
35-
});
36-
37-
try {
38-
const activeSpan = getActiveSpan();
39-
const rootSpan = activeSpan && getRootSpan(activeSpan);
40-
41-
const response: Response = await handleCallbackErrors(
42-
() => originalFunction.apply(thisArg, args),
43-
error => {
44-
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
45-
if (isRedirectNavigationError(error)) {
46-
// Don't do anything
47-
} else if (isNotFoundNavigationError(error) && rootSpan) {
48-
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
49-
} else {
50-
captureException(error, {
51-
mechanism: {
52-
handled: false,
53-
},
54-
});
55-
}
31+
return withIsolationScope(async isolationScope => {
32+
isolationScope.setSDKProcessingMetadata({
33+
request: {
34+
headers: headers ? winterCGHeadersToDict(headers) : undefined,
5635
},
57-
);
36+
});
5837

5938
try {
60-
if (rootSpan && response.status) {
61-
setHttpStatus(rootSpan, response.status);
39+
const activeSpan = getActiveSpan();
40+
const rootSpan = activeSpan && getRootSpan(activeSpan);
41+
42+
const response: Response = await handleCallbackErrors(
43+
() => originalFunction.apply(thisArg, args),
44+
error => {
45+
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
46+
if (isRedirectNavigationError(error)) {
47+
// Don't do anything
48+
} else if (isNotFoundNavigationError(error) && rootSpan) {
49+
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
50+
} else {
51+
captureException(error, {
52+
mechanism: {
53+
handled: false,
54+
},
55+
});
56+
}
57+
},
58+
);
59+
60+
try {
61+
if (rootSpan && response.status) {
62+
setHttpStatus(rootSpan, response.status);
63+
}
64+
} catch {
65+
// best effort - response may be undefined?
6266
}
63-
} catch {
64-
// best effort - response may be undefined?
65-
}
6667

67-
return response;
68-
} finally {
69-
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
70-
// 1. Edge transport requires manual flushing
71-
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
72-
await flushQueue();
68+
return response;
69+
} finally {
70+
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
71+
// 1. Edge transport requires manual flushing
72+
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
73+
await flushQueue();
74+
}
7375
}
74-
}
76+
});
7577
},
7678
});
7779
}

packages/nextjs/src/server/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export function init(options: NodeOptions): void {
7575
...getDefaultIntegrations(options).filter(
7676
integration =>
7777
integration.name !== 'OnUncaughtException' &&
78-
// Next.js comes with its own Node-Fetch and Http instrumentation, so we shouldn't add ours on-top
78+
// Next.js comes with its own Node-Fetch instrumentation, so we shouldn't add ours on-top
7979
integration.name !== 'NodeFetch' &&
80+
// Next.js comes with its own Http instrumentation for OTel which lead to double spans for route handler requests
8081
integration.name !== 'Http',
8182
),
8283
onUncaughtExceptionIntegration(),

0 commit comments

Comments
 (0)