Skip to content

fix(browser): Avoid 4xx response for succesful diagnoseSdkConnectivity request #16840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/browser/src/diagnose-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ export async function diagnoseSdkConnectivity(): Promise<
try {
// If fetch throws, there is likely an ad blocker active or there are other connective issues.
await fetch(
// We want this to be as close as possible to an actual ingest URL so that ad blockers will actually block the request
// We are using the "sentry-sdks" org with id 447951 not to pollute any actual organizations.
'https://o447951.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.browser%2F1.33.7',
// We are using the
// - "sentry-sdks" org with id 447951 not to pollute any actual organizations.
// - "diagnose-sdk-connectivity" project with id 4509632503087104
// - the public key of said org/project, which is disabled in the project settings
// => this DSN: https://[email protected]/4509632503087104 (i.e. disabled)
'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7',
{
body: '{}',
method: 'POST',
Expand Down
165 changes: 165 additions & 0 deletions packages/browser/test/diagnose-sdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* @vitest-environment jsdom
*/

import type { Client } from '@sentry/core';
import * as sentryCore from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { diagnoseSdkConnectivity } from '../src/diagnose-sdk';

// Mock the @sentry/core module
vi.mock('@sentry/core', async requireActual => {
return {
...((await requireActual()) as any),
getClient: vi.fn(),
};
});

// Mock global fetch
const mockFetch = vi.fn();
global.fetch = mockFetch;

describe('diagnoseSdkConnectivity', () => {
const mockGetClient = sentryCore.getClient as any;

beforeEach(() => {
vi.clearAllMocks();
});

afterEach(() => {
vi.clearAllMocks();
});

it('returns "no-client-active" when no client is active', async () => {
mockGetClient.mockReturnValue(undefined);

const result = await diagnoseSdkConnectivity();

expect(result).toBe('no-client-active');
expect(mockFetch).not.toHaveBeenCalled();
});

it('returns "no-dsn-configured" when client.getDsn() returns undefined', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue(undefined),
};
mockGetClient.mockReturnValue(mockClient);

const result = await diagnoseSdkConnectivity();

expect(result).toBe('no-dsn-configured');
expect(mockClient.getDsn).toHaveBeenCalled();
expect(mockFetch).not.toHaveBeenCalled();
});

it('returns "sentry-unreachable" when fetch throws an error', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
mockFetch.mockRejectedValue(new Error('Network error'));

const result = await diagnoseSdkConnectivity();

expect(result).toBe('sentry-unreachable');
expect(mockClient.getDsn).toHaveBeenCalled();
expect(mockFetch).toHaveBeenCalledWith(
'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7',
{
body: '{}',
method: 'POST',
mode: 'cors',
credentials: 'omit',
},
);
});

it('returns "sentry-unreachable" when fetch throws a TypeError (common for network issues)', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
mockFetch.mockRejectedValue(new TypeError('Failed to fetch'));

const result = await diagnoseSdkConnectivity();

expect(result).toBe('sentry-unreachable');
expect(mockClient.getDsn).toHaveBeenCalled();
expect(mockFetch).toHaveBeenCalled();
});

it('returns undefined when connectivity check succeeds', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
mockFetch.mockResolvedValue(new Response('{}', { status: 200 }));

const result = await diagnoseSdkConnectivity();

expect(result).toBeUndefined();
expect(mockClient.getDsn).toHaveBeenCalled();
expect(mockFetch).toHaveBeenCalledWith(
'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7',
{
body: '{}',
method: 'POST',
mode: 'cors',
credentials: 'omit',
},
);
});

it('returns undefined even when fetch returns an error status (4xx, 5xx)', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
// Mock a 403 response (expected since the DSN is disabled)
mockFetch.mockResolvedValue(new Response('Forbidden', { status: 403 }));

const result = await diagnoseSdkConnectivity();

// The function only cares about fetch not throwing, not the response status
expect(result).toBeUndefined();
expect(mockClient.getDsn).toHaveBeenCalled();
expect(mockFetch).toHaveBeenCalled();
});

it('uses the correct test endpoint URL', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
mockFetch.mockResolvedValue(new Response('{}', { status: 200 }));

await diagnoseSdkConnectivity();

expect(mockFetch).toHaveBeenCalledWith(
'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7',
expect.objectContaining({
body: '{}',
method: 'POST',
mode: 'cors',
credentials: 'omit',
}),
);
});

it('uses correct fetch options', async () => {
const mockClient: Partial<Client> = {
getDsn: vi.fn().mockReturnValue('https://[email protected]/123'),
};
mockGetClient.mockReturnValue(mockClient);
mockFetch.mockResolvedValue(new Response('{}', { status: 200 }));

await diagnoseSdkConnectivity();

expect(mockFetch).toHaveBeenCalledWith(expect.any(String), {
body: '{}',
method: 'POST',
mode: 'cors',
credentials: 'omit',
});
});
});
Loading