Skip to content

Commit 1a9e6f6

Browse files
committed
restrict try to original route handler
1 parent 94aebe5 commit 1a9e6f6

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

packages/nextjs/src/utils/handlers.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,50 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
3232
// return a value. In our case, all any of the codepaths return is a promise of `void`, but nextjs still counts on
3333
// getting that before it will finish the response.
3434
const boundHandler = local.bind(async () => {
35-
try {
36-
const currentScope = getCurrentHub().getScope();
35+
const currentScope = getCurrentHub().getScope();
3736

38-
if (currentScope) {
39-
currentScope.addEventProcessor(event => addRequestDataToEvent(event, req as NextRequest));
37+
if (currentScope) {
38+
currentScope.addEventProcessor(event => addRequestDataToEvent(event, req as NextRequest));
4039

41-
if (hasTracingEnabled()) {
42-
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
43-
let traceparentData;
44-
if (req.headers && isString(req.headers['sentry-trace'])) {
45-
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
46-
logger.log(`[Tracing] Continuing trace ${traceparentData?.traceId}.`);
47-
}
40+
if (hasTracingEnabled()) {
41+
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
42+
let traceparentData;
43+
if (req.headers && isString(req.headers['sentry-trace'])) {
44+
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
45+
logger.log(`[Tracing] Continuing trace ${traceparentData?.traceId}.`);
46+
}
4847

49-
const url = `${req.url}`;
50-
// pull off query string, if any
51-
let reqPath = stripUrlQueryAndFragment(url);
52-
// Replace with placeholder
53-
if (req.query) {
54-
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
55-
// they match dynamic parts
56-
for (const [key, value] of Object.entries(req.query)) {
57-
reqPath = reqPath.replace(`${value}`, `[${key}]`);
58-
}
48+
const url = `${req.url}`;
49+
// pull off query string, if any
50+
let reqPath = stripUrlQueryAndFragment(url);
51+
// Replace with placeholder
52+
if (req.query) {
53+
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
54+
// they match dynamic parts
55+
for (const [key, value] of Object.entries(req.query)) {
56+
reqPath = reqPath.replace(`${value}`, `[${key}]`);
5957
}
60-
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
61-
62-
const transaction = startTransaction(
63-
{
64-
name: `${reqMethod}${reqPath}`,
65-
op: 'http.server',
66-
...traceparentData,
67-
},
68-
// extra context passed to the `tracesSampler`
69-
{ request: req },
70-
);
71-
currentScope.setSpan(transaction);
72-
73-
// save a link to the scope on the response, so that even if there's an error (landing us outside of
74-
// the domain), we can still finish the transaction and attach the correct data to it
75-
(res as ScopedResponse).__sentryScope = currentScope;
7658
}
59+
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
60+
61+
const transaction = startTransaction(
62+
{
63+
name: `${reqMethod}${reqPath}`,
64+
op: 'http.server',
65+
...traceparentData,
66+
},
67+
// extra context passed to the `tracesSampler`
68+
{ request: req },
69+
);
70+
currentScope.setSpan(transaction);
71+
72+
// save a link to the scope on the response, so that even if there's an error (landing us outside of
73+
// the domain), we can still finish the transaction and attach the correct data to it
74+
(res as ScopedResponse).__sentryScope = currentScope;
7775
}
76+
}
7877

78+
try {
7979
return await handler(req, res); // Call original handler
8080
} catch (e) {
8181
withScope(scope => {

0 commit comments

Comments
 (0)