Skip to content

Commit d804d14

Browse files
authored
chore(sentry): improve log for non error exceptions (#15)
1 parent fd06af8 commit d804d14

File tree

7 files changed

+30
-13
lines changed

7 files changed

+30
-13
lines changed

lib/__test__/script.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ describe('fetchScript', () => {
6363
});
6464

6565
it('should reject when onerror is called', async () => {
66-
spyOnError.set.mockImplementationOnce((callback: (any) => void) => {
67-
callback({event: 'test'});
66+
spyOnError.set.mockImplementationOnce((callback: (any) => any) => {
67+
callback('Invalid Script');
6868
});
6969

70-
await expect(fetchScript()).rejects.toThrow(SCRIPT_ERROR);
70+
await expect(fetchScript()).rejects.toBe('Invalid Script');
7171

7272
expect(spyOnAppend).toHaveBeenCalled();
7373
expect(spyOnRemove).toHaveBeenCalled();

lib/__test__/sentry.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getSentry,
77
getSentryHubWrapper,
88
} from '../src/sentry';
9+
import { SCRIPT_ERROR } from '../src/constants.js';
910

1011
jest.mock('@sentry/browser', () => ({
1112
BrowserClient: jest.fn(),
@@ -64,7 +65,7 @@ describe('Sentry', () => {
6465
const mockHub = {
6566
addBreadcrumb: jest.fn(),
6667
captureMessage: jest.fn(),
67-
captureException: jest.fn(),
68+
captureEvent: jest.fn(),
6869
withScope: jest.fn(callback => callback(mockScope)),
6970
};
7071

@@ -79,8 +80,19 @@ describe('Sentry', () => {
7980
sentryHubWrapper.captureMessage('test message');
8081
expect(mockHub.captureMessage).toHaveBeenCalledWith('test message');
8182

82-
sentryHubWrapper.captureException('test exception');
83-
expect(mockHub.captureException).toHaveBeenCalledWith('test exception');
83+
sentryHubWrapper.captureException(new Error('test error'));
84+
expect(mockHub.captureEvent).toHaveBeenCalledWith({
85+
message: SCRIPT_ERROR,
86+
level: 'error',
87+
extra: new Error('test error'),
88+
});
89+
90+
sentryHubWrapper.captureException('test non error');
91+
expect(mockHub.captureEvent).toHaveBeenCalledWith({
92+
message: SCRIPT_ERROR,
93+
level: 'error',
94+
extra: 'test non error',
95+
});
8496
});
8597
});
8698

lib/src/loader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
7373
sentry.addBreadcrumb({
7474
category: 'hCaptcha:script',
7575
message: 'hCaptcha failed to load',
76+
data: error,
7677
});
7778

7879
sentry.captureException(error);

lib/src/script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ERROR, SCRIPT_ID } from './constants';
1+
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ID } from './constants';
22
import { getFrame, getMountElement } from './utils';
33

44
import type { IScriptParams } from './types';
@@ -34,7 +34,7 @@ export function fetchScript({
3434
};
3535

3636
script.onload = (event) => onComplete(event, resolve);
37-
script.onerror = (event) => onComplete(event, () => reject(new Error(SCRIPT_ERROR)));
37+
script.onerror = (event) => onComplete(event, reject);
3838

3939
script.src += query !== '' ? `&${query}` : '';
4040

lib/src/sentry.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Sentry from '@sentry/browser';
22

33
import { ScopeTag, SentryHub } from './types';
4+
import { SCRIPT_ERROR } from './constants';
45

56
const SENTRY_DSN = process.env.SENTRY_DSN_TOKEN;
67

@@ -71,15 +72,18 @@ export function getSentryHubWrapper(
7172
sentryHub.captureMessage(message);
7273
});
7374
},
74-
captureException: (e) => {
75+
captureException: (error) => {
7576
if (!sentryHub) {
7677
return;
7778
}
7879

7980
sentryHub.withScope(function (scope) {
8081
scope.setTag(tag.key, tag.value);
81-
82-
sentryHub.captureException(e);
82+
sentryHub.captureEvent({
83+
message: SCRIPT_ERROR,
84+
level: 'error',
85+
extra: error
86+
});
8387
});
8488
}
8589
};

lib/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface ILoaderParams extends IScriptParams {
2323

2424
export interface SentryHub {
2525
addBreadcrumb: (breadcrumb: object) => void;
26-
captureException: (e: any) => void;
26+
captureException: (error: any) => void;
2727
captureMessage: (message: string) => void;
2828
}
2929

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hcaptcha/loader",
33
"description": "This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling.",
4-
"version": "1.0.9",
4+
"version": "1.0.10",
55
"author": "hCaptcha team and contributors",
66
"license": "MIT",
77
"keywords": [

0 commit comments

Comments
 (0)