Skip to content

Commit b262a70

Browse files
s1gr1dmydea
authored andcommitted
review comments
1 parent ae8a747 commit b262a70

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';
@@ -29,50 +29,52 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
2929

3030
return new Proxy(routeHandler, {
3131
apply: async (originalFunction, thisArg, args) => {
32-
getIsolationScope().setSDKProcessingMetadata({
33-
request: {
34-
headers: headers ? winterCGHeadersToDict(headers) : undefined,
35-
},
36-
});
37-
38-
try {
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-
}
32+
return withIsolationScope(async isolationScope => {
33+
isolationScope.setSDKProcessingMetadata({
34+
request: {
35+
headers: headers ? winterCGHeadersToDict(headers) : undefined,
5736
},
58-
);
37+
});
5938

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

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();
69+
return response;
70+
} finally {
71+
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
72+
// 1. Edge transport requires manual flushing
73+
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
74+
await flushQueue();
75+
}
7476
}
75-
}
77+
});
7678
},
7779
});
7880
}

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)