Skip to content

Commit cea775f

Browse files
committed
fix(nextjs): Remove circular dependency
``` yarn run v1.22.5 $ madge --circular src/index.client.ts && madge --circular src/index.server.ts Processed 5 files (794ms) (7 warnings) ✔ No circular dependency found! Processed 7 files (798ms) (9 warnings) ✖ Found 1 circular dependency! 1) index.server.ts > utils/handlers.ts > utils/instrumentServer.ts ``` To remove the circular dependency between index.server.ts and utils/instrumentServer.ts, we rely on importing Sentry func (like startTransaction and captureException) from `@sentry/node` directly.
1 parent 0772d36 commit cea775f

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

packages/nextjs/src/utils/instrumentServer.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepReadDirSync } from '@sentry/node';
1+
import { captureException, deepReadDirSync, getCurrentHub, startTransaction } from '@sentry/node';
22
import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
33
import { Event as SentryEvent } from '@sentry/types';
44
import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils';
@@ -8,8 +8,6 @@ import { default as createNextServer } from 'next';
88
import * as querystring from 'querystring';
99
import * as url from 'url';
1010

11-
import * as Sentry from '../index.server';
12-
1311
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1412
type PlainObject<T = any> = { [key: string]: T };
1513

@@ -156,7 +154,7 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand
156154
function makeWrappedErrorLogger(origErrorLogger: ErrorLogger): WrappedErrorLogger {
157155
return function(this: Server, err: Error): void {
158156
// TODO add context data here
159-
Sentry.captureException(err);
157+
captureException(err);
160158
return origErrorLogger.call(this, err);
161159
};
162160
}
@@ -201,7 +199,7 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {
201199
// local.on('error', Sentry.captureException);
202200

203201
local.run(() => {
204-
const currentScope = Sentry.getCurrentHub().getScope();
202+
const currentScope = getCurrentHub().getScope();
205203

206204
if (currentScope) {
207205
currentScope.addEventProcessor(event => addRequestDataToEvent(event, req));
@@ -222,7 +220,7 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {
222220
// name; requests to API routes could be GET, POST, PUT, etc, so do include it there
223221
const namePrefix = req.url.startsWith('/api') ? `${(req.method || 'GET').toUpperCase()} ` : '';
224222

225-
const transaction = Sentry.startTransaction(
223+
const transaction = startTransaction(
226224
{
227225
name: `${namePrefix}${reqPath}`,
228226
op: 'http.server',

0 commit comments

Comments
 (0)