Skip to content

Commit 675309d

Browse files
authored
feat(core): Expose isInitialized() to replace checking via getClient (#10296)
Currently, you can use `Sentry.getClient() !== undefined` to check if Sentry was initialized. In v8, we want to change this so that this _always_ returns a client (possibly a Noop client), so this check will not work anymore there. Instead, we can provide a new util that does this explicitly, where we can control what it checks under the hood.
1 parent a0b987a commit 675309d

File tree

15 files changed

+48
-2
lines changed

15 files changed

+48
-2
lines changed

MIGRATION.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ npx @sentry/migr8@latest
1010
This will let you select which updates to run, and automatically update your code. Make sure to still review all code
1111
changes!
1212

13+
## Deprecate using `getClient()` to check if the SDK was initialized
14+
15+
In v8, `getClient()` will stop returning `undefined` if `Sentry.init()` was not called. For cases where this may be used
16+
to check if Sentry was actually initialized, using `getClient()` will thus not work anymore. Instead, you should use the
17+
new `Sentry.isInitialized()` utility to check this.
18+
1319
## Deprecate `getCurrentHub()`
1420

1521
In v8, you will no longer have a Hub, only Scopes as a concept. This also means that `getCurrentHub()` will eventually

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
// eslint-disable-next-line deprecation/deprecation
2929
getCurrentHub,
3030
getClient,
31+
isInitialized,
3132
getCurrentScope,
3233
getGlobalScope,
3334
getIsolationScope,

packages/browser/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export {
4040
// eslint-disable-next-line deprecation/deprecation
4141
getCurrentHub,
4242
getClient,
43+
isInitialized,
4344
getCurrentScope,
4445
Hub,
4546
// eslint-disable-next-line deprecation/deprecation

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export {
4545
// eslint-disable-next-line deprecation/deprecation
4646
getCurrentHub,
4747
getClient,
48+
isInitialized,
4849
getCurrentScope,
4950
getGlobalScope,
5051
getIsolationScope,

packages/core/src/exports.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ export function getClient<C extends Client>(): C | undefined {
394394
return getCurrentHub().getClient<C>();
395395
}
396396

397+
/**
398+
* Returns true if Sentry has been properly initialized.
399+
*/
400+
export function isInitialized(): boolean {
401+
return !!getClient();
402+
}
403+
397404
/**
398405
* Get the currently active scope.
399406
*/

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export {
3131
withScope,
3232
withIsolationScope,
3333
getClient,
34+
isInitialized,
3435
getCurrentScope,
3536
startSession,
3637
endSession,

packages/core/test/lib/exports.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GLOBAL_OBJ } from '@sentry/utils';
12
import {
23
Hub,
34
Scope,
@@ -16,6 +17,7 @@ import {
1617
withIsolationScope,
1718
withScope,
1819
} from '../../src';
20+
import { isInitialized } from '../../src/exports';
1921
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
2022

2123
function getTestClient(): TestClient {
@@ -315,3 +317,19 @@ describe('session APIs', () => {
315317
});
316318
});
317319
});
320+
321+
describe('isInitialized', () => {
322+
it('returns false if no client is setup', () => {
323+
GLOBAL_OBJ.__SENTRY__.hub = undefined;
324+
expect(isInitialized()).toBe(false);
325+
});
326+
327+
it('returns true if client is setup', () => {
328+
const client = getTestClient();
329+
const hub = new Hub(client);
330+
// eslint-disable-next-line deprecation/deprecation
331+
makeMain(hub);
332+
333+
expect(isInitialized()).toBe(true);
334+
});
335+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export {
4444
// eslint-disable-next-line deprecation/deprecation
4545
getCurrentHub,
4646
getClient,
47+
isInitialized,
4748
getCurrentScope,
4849
getGlobalScope,
4950
getIsolationScope,

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type { Span } from './types';
1717
export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan } from '@sentry/opentelemetry';
1818
export {
1919
getClient,
20+
isInitialized,
2021
addBreadcrumb,
2122
captureException,
2223
captureEvent,

packages/node-experimental/src/sdk/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { getContextFromScope, getScopesFromContext, setScopesOnContext } from '.
2222
import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
2323
import { parseEventHintOrCaptureContext } from '../utils/prepareEvent';
2424
import type { Scope } from './scope';
25-
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from './scope';
25+
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope, isInitialized } from './scope';
2626

27-
export { getCurrentScope, getGlobalScope, getIsolationScope, getClient };
27+
export { getCurrentScope, getGlobalScope, getIsolationScope, getClient, isInitialized };
2828
export { setCurrentScope, setIsolationScope } from './scope';
2929

3030
/**

0 commit comments

Comments
 (0)