Skip to content

Commit db3f220

Browse files
authored
chore(lib): move sentryhub guard to wrapper (#10)
1 parent 6999541 commit db3f220

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

lib/__test__/sentry.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ describe('Sentry', () => {
3838
expect(hub).toBeTruthy();
3939
});
4040

41-
it('should return null if Sentry is disabled', () => {
42-
const hub = initSentry(false);
43-
expect(Sentry.BrowserClient).not.toHaveBeenCalled();
44-
expect(Sentry.Hub).not.toHaveBeenCalled();
45-
expect(hub).toBeNull();
41+
it('should not throw when Sentry is false', () => {
42+
const sentryHubWrapper = getSentryHubWrapper(false);
43+
44+
const testWrapperCall = () => {
45+
sentryHubWrapper.addBreadcrumb({ category: 'test' });
46+
sentryHubWrapper.captureMessage('test message');
47+
sentryHubWrapper.captureException('test error');
48+
};
49+
50+
expect(testWrapperCall).not.toThrow();
4651
});
4752

4853
it('should get initialized Sentry Hub', () => {
@@ -64,7 +69,7 @@ describe('Sentry', () => {
6469
};
6570

6671
const tag = { key: 'testKey', value: 'testValue' };
67-
const breadcrumb = { category: 'test breadcrumb' };
72+
const breadcrumb = { category: 'test' };
6873

6974
const sentryHubWrapper = getSentryHubWrapper(mockHub, tag);
7075

lib/src/loader.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
1414

1515
try {
1616

17-
sentry?.addBreadcrumb({
17+
sentry.addBreadcrumb({
1818
category: 'script',
1919
message: 'hCaptcha loader params',
2020
data: params,
@@ -25,7 +25,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
2525
const script = hCaptchaScripts.find(({ scope }) => scope === frame.window);
2626

2727
if (script) {
28-
sentry?.addBreadcrumb({
28+
sentry.addBreadcrumb({
2929
category: 'script',
3030
message: 'hCaptcha already loaded',
3131
});
@@ -41,7 +41,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
4141

4242
// Create global onload callback for the hCaptcha library to call
4343
frame.window[HCAPTCHA_LOAD_FN_NAME] = () => {
44-
sentry?.addBreadcrumb({
44+
sentry.addBreadcrumb({
4545
category: 'hCaptcha:script',
4646
message: 'hCaptcha script called onload function',
4747
});
@@ -65,16 +65,16 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
6565

6666
await fetchScript({ query, ...params });
6767

68-
sentry?.addBreadcrumb({
68+
sentry.addBreadcrumb({
6969
category: 'hCaptcha:script',
7070
message: 'hCaptcha loaded',
7171
});
7272
} catch(error) {
73-
sentry?.addBreadcrumb({
73+
sentry.addBreadcrumb({
7474
category: 'hCaptcha:script',
7575
message: 'hCaptcha failed to load',
7676
});
77-
sentry?.captureException(error);
77+
sentry.captureException(error);
7878
reject(new Error(SCRIPT_ERROR));
7979
}
8080
}
@@ -83,8 +83,8 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
8383
hCaptchaScripts.push({ promise, scope: frame.window });
8484

8585
return promise;
86-
} catch (e) {
87-
sentry?.captureException(e);
86+
} catch (error) {
87+
sentry.captureException(error);
8888
return Promise.reject(new Error(SCRIPT_ERROR));
8989
}
9090
}

lib/src/sentry.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function initSentry(
1313

1414
// Sentry disabled in params
1515
if (sentry === false) {
16-
return null;
16+
return getSentryHubWrapper(sentry);
1717
}
1818

1919
// Client was already created
@@ -52,15 +52,30 @@ export function getSentryHubWrapper(
5252
}): SentryHub {
5353

5454
return {
55-
addBreadcrumb: (breadcrumb) => sentryHub.addBreadcrumb(breadcrumb),
55+
56+
addBreadcrumb: (breadcrumb) => {
57+
if (!sentryHub) {
58+
return;
59+
}
60+
61+
sentryHub.addBreadcrumb(breadcrumb);
62+
},
5663
captureMessage: (message) => {
64+
if (!sentryHub) {
65+
return;
66+
}
67+
5768
sentryHub.withScope(function (scope) {
5869
scope.setTag(tag.key, tag.value);
5970

6071
sentryHub.captureMessage(message);
6172
});
6273
},
6374
captureException: (e) => {
75+
if (!sentryHub) {
76+
return;
77+
}
78+
6479
sentryHub.withScope(function (scope) {
6580
scope.setTag(tag.key, tag.value);
6681

0 commit comments

Comments
 (0)