Skip to content

Commit cec610c

Browse files
author
Luca Forstner
committed
feat(browser): Add diagnoseSdk() function to programmatically detect possible issues
1 parent 2ed6fc2 commit cec610c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getCurrentScope } from '@sentry/core';
2+
3+
/**
4+
* A function to diagnose why the SDK might not be successfully sending data.
5+
*
6+
* Possible return values wrapped in a Promise:
7+
* - `"no-client-active"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet.
8+
* - `"sentry-unreachable"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet.
9+
*/
10+
export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreachable' | void> {
11+
const client = getCurrentScope().getClient();
12+
13+
if (!client) {
14+
return 'no-client-active';
15+
}
16+
17+
try {
18+
await fetch(
19+
'https://o1337.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7',
20+
{
21+
body: '',
22+
method: 'POST',
23+
mode: 'cors',
24+
credentials: 'omit',
25+
},
26+
);
27+
} catch {
28+
return 'sentry-unreachable';
29+
}
30+
}

packages/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './int
7171
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
7272
export { unleashIntegration } from './integrations/featureFlags/unleash';
7373
export { statsigIntegration } from './integrations/featureFlags/statsig';
74+
export { diagnoseSdk } from './diagnose-sdk';

0 commit comments

Comments
 (0)