Skip to content

Commit 440f7ac

Browse files
committed
- move tunnel check from @sentry/nextjs to @sentry/node
- Use `console.warn` instead of `logger.warn` - Do not re-throw the error
1 parent c04cf80 commit 440f7ac

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

packages/nextjs/src/index.server.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ export function init(options: NextjsOptions): void {
7979
const activeDomain = domain.active;
8080
domain.active = null;
8181

82-
if (typeof options.tunnel !== 'undefined') {
83-
try {
84-
new URL(options.tunnel);
85-
} catch (error) {
86-
__DEBUG_BUILD__ &&
87-
logger.error('The tunnel option is not a valid URL. It must be a full URL including the protocol.');
88-
throw new Error('The tunnel option is not a valid URL. It must be a full URL including the protocol.');
89-
}
90-
}
9182
nodeInit(options);
9283

9384
const filterTransactions: EventProcessor = event => {

packages/nextjs/test/index.server.test.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,29 @@ describe('Server init()', () => {
140140
});
141141

142142
it('should not fail on valid tunnel option', () => {
143+
const consoleWarnSpy = jest.spyOn(console, 'warn');
143144
expect(() =>
144145
init({
145146
dsn: 'https://[email protected]/12312012',
146147
tunnel: 'https://example.com/api',
147148
}),
148149
).not.toThrowError();
150+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
151+
'The tunnel option is not a valid URL. It must be a full URL including the protocol when using the Node transport.',
152+
);
149153
});
150154

151-
it('should fail on invalid tunnel option', () => {
152-
expect(() =>
153-
init({
154-
dsn: 'https://[email protected]/12312012',
155-
tunnel: '/invalid',
156-
}),
157-
).toThrowError('The tunnel option is not a valid URL. It must be a full URL including the protocol.');
155+
it('should not fail on invalid tunnel option', () => {
156+
const consoleWarnSpy = jest.spyOn(console, 'warn');
157+
158+
init({
159+
dsn: 'https://[email protected]/12312012',
160+
tunnel: '/invalid',
161+
});
162+
163+
expect(consoleWarnSpy).toHaveBeenCalledWith(
164+
'The tunnel option is not a valid URL. It must be a full URL including the protocol when using the Node transport.',
165+
);
158166
});
159167

160168
describe('integrations', () => {

packages/node/src/sdk.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export function init(options: NodeOptions = {}): void {
157157
setHubOnCarrier(carrier, getCurrentHub());
158158
}
159159

160+
checkNodeTunnelOption(options);
161+
160162
// TODO(v7): Refactor this to reduce the logic above
161163
const clientOptions: NodeClientOptions = {
162164
...options,
@@ -172,6 +174,20 @@ export function init(options: NodeOptions = {}): void {
172174
}
173175
}
174176

177+
function checkNodeTunnelOption(options: NodeOptions = {}): void {
178+
if (options.tunnel !== undefined) {
179+
try {
180+
new URL(options.tunnel);
181+
} catch (error) {
182+
// eslint-disable-next-line no-console
183+
console.warn(
184+
'The tunnel option is not a valid URL. It must be a full URL including the protocol when using the Node transport.',
185+
);
186+
delete options.tunnel;
187+
}
188+
}
189+
}
190+
175191
/**
176192
* This is the getter for lastEventId.
177193
*

packages/node/test/sdk.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const defaultIntegrationsBackup = sdk.defaultIntegrations;
2020

2121
describe('init()', () => {
2222
beforeEach(() => {
23+
jest.clearAllMocks();
2324
global.__SENTRY__ = {};
2425
});
2526

@@ -89,4 +90,28 @@ describe('init()', () => {
8990
expect(mockDefaultIntegrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
9091
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
9192
});
93+
94+
it('should not error on valid tunnel option', () => {
95+
const consoleWarnSpy = jest.spyOn(console, 'warn');
96+
expect(() =>
97+
init({
98+
dsn: 'https://[email protected]/12312012',
99+
tunnel: 'https://example.com/api',
100+
}),
101+
).not.toThrowError();
102+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
103+
'The tunnel option is not a valid URL. It must be a full URL including the protocol when using the Node transport.',
104+
);
105+
});
106+
107+
it('should not error on invalid tunnel option', () => {
108+
const consoleWarnSpy = jest.spyOn(console, 'warn');
109+
init({
110+
dsn: 'https://[email protected]/12312012',
111+
tunnel: '/invalid',
112+
});
113+
expect(consoleWarnSpy).toHaveBeenCalledWith(
114+
'The tunnel option is not a valid URL. It must be a full URL including the protocol when using the Node transport.',
115+
);
116+
});
92117
});

0 commit comments

Comments
 (0)