Skip to content

Commit bd96876

Browse files
authored
ref(nextjs): Remove dead code (#13903)
Cleans up the instrumentation for pages router API routes and removes some dead code.
1 parent 4ad2c35 commit bd96876

File tree

3 files changed

+23
-45
lines changed

3 files changed

+23
-45
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
99
## Unreleased
1010

11+
### Important Changes
12+
13+
- **ref(nextjs): Remove dead code ([#13828](https://github.com/getsentry/sentry-javascript/pull/13903))**
14+
15+
Relevant for users of the `@sentry/nextjs` package: If you have previously configured a
16+
`SENTRY_IGNORE_API_RESOLUTION_ERROR` environment variable, it is now safe to unset it.
17+
1118
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
1219

1320
Work in this release was contributed by @trzeciak and @lizhiyao. Thank you for your contributions!

packages/nextjs/src/common/types.ts

-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ export type WrappedNextApiHandler = {
5555
__sentry_route__?: string;
5656
__sentry_wrapped__?: boolean;
5757
};
58-
59-
export type AugmentedNextApiRequest = NextApiRequest & {
60-
__withSentry_applied__?: boolean;
61-
};
62-
6358
export type AugmentedNextApiResponse = NextApiResponse & {
6459
__sentryTransaction?: SentrySpan;
6560
};

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

+16-40
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import {
66
startSpanManual,
77
withIsolationScope,
88
} from '@sentry/core';
9-
import { consoleSandbox, isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
10-
119
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
12-
import type { AugmentedNextApiRequest, AugmentedNextApiResponse, NextApiHandler } from './types';
10+
import { isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
11+
import type { NextApiRequest } from 'next';
12+
import type { AugmentedNextApiResponse, NextApiHandler } from './types';
1313
import { flushSafelyWithTimeout } from './utils/responseEnd';
1414
import { escapeNextjsTracing } from './utils/tracingUtils';
1515

16+
export type AugmentedNextApiRequest = NextApiRequest & {
17+
__withSentry_applied__?: boolean;
18+
};
19+
1620
/**
17-
* Wrap the given API route handler for tracing and error capturing. Thin wrapper around `withSentry`, which only
18-
* applies it if it hasn't already been applied.
21+
* Wrap the given API route handler with error nad performance monitoring.
1922
*
2023
* @param apiHandler The handler exported from the user's API page route file, which may or may not already be
2124
* wrapped with `withSentry`
2225
* @param parameterizedRoute The page's parameterized route.
23-
* @returns The wrapped handler
26+
* @returns The wrapped handler which will always return a Promise.
2427
*/
2528
export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameterizedRoute: string): NextApiHandler {
2629
return new Proxy(apiHandler, {
@@ -44,9 +47,7 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
4447
return wrappingTarget.apply(thisArg, args);
4548
}
4649

47-
// We're now auto-wrapping API route handlers using `wrapApiHandlerWithSentry` (which uses `withSentry` under the hood), but
48-
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
49-
// idempotent so that those cases don't break anything.
50+
// Prevent double wrapping of the same request.
5051
if (req.__withSentry_applied__) {
5152
return wrappingTarget.apply(thisArg, args);
5253
}
@@ -55,7 +56,6 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
5556
return withIsolationScope(isolationScope => {
5657
return continueTrace(
5758
{
58-
// TODO(v8): Make it so that continue trace will allow null as sentryTrace value and remove this fallback here
5959
sentryTrace:
6060
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined,
6161
baggage: req.headers?.baggage,
@@ -80,31 +80,15 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
8080
// eslint-disable-next-line @typescript-eslint/unbound-method
8181
res.end = new Proxy(res.end, {
8282
apply(target, thisArg, argArray) {
83-
if (span.isRecording()) {
84-
setHttpStatus(span, res.statusCode);
85-
span.end();
86-
}
83+
setHttpStatus(span, res.statusCode);
84+
span.end();
8785
vercelWaitUntil(flushSafelyWithTimeout());
88-
target.apply(thisArg, argArray);
86+
return target.apply(thisArg, argArray);
8987
},
9088
});
9189

9290
try {
93-
const handlerResult = await wrappingTarget.apply(thisArg, args);
94-
if (
95-
process.env.NODE_ENV === 'development' &&
96-
!process.env.SENTRY_IGNORE_API_RESOLUTION_ERROR &&
97-
!res.writableEnded
98-
) {
99-
consoleSandbox(() => {
100-
// eslint-disable-next-line no-console
101-
console.warn(
102-
'[sentry] If Next.js logs a warning "API resolved without sending a response", it\'s a false positive, which may happen when you use `wrapApiHandlerWithSentry` manually to wrap your routes. To suppress this warning, set `SENTRY_IGNORE_API_RESOLUTION_ERROR` to 1 in your env. To suppress the nextjs warning, use the `externalResolver` API route option (see https://nextjs.org/docs/api-routes/api-middlewares#custom-config for details).',
103-
);
104-
});
105-
}
106-
107-
return handlerResult;
91+
return await wrappingTarget.apply(thisArg, args);
10892
} catch (e) {
10993
// In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can
11094
// store a seen flag on it. (Because of the one-way-on-Vercel-one-way-off-of-Vercel approach we've been forced
@@ -123,16 +107,8 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
123107
},
124108
});
125109

126-
// Because we're going to finish and send the transaction before passing the error onto nextjs, it won't yet
127-
// have had a chance to set the status to 500, so unless we do it ourselves now, we'll incorrectly report that
128-
// the transaction was error-free
129-
res.statusCode = 500;
130-
res.statusMessage = 'Internal Server Error';
131-
132-
if (span.isRecording()) {
133-
setHttpStatus(span, res.statusCode);
134-
span.end();
135-
}
110+
setHttpStatus(span, 500);
111+
span.end();
136112

137113
vercelWaitUntil(flushSafelyWithTimeout());
138114

0 commit comments

Comments
 (0)