Skip to content

Commit

Permalink
chore(sentry): improve log for non error exceptions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
faris-imi authored Nov 22, 2023
1 parent fd06af8 commit d804d14
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/__test__/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ describe('fetchScript', () => {
});

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

await expect(fetchScript()).rejects.toThrow(SCRIPT_ERROR);
await expect(fetchScript()).rejects.toBe('Invalid Script');

expect(spyOnAppend).toHaveBeenCalled();
expect(spyOnRemove).toHaveBeenCalled();
Expand Down
18 changes: 15 additions & 3 deletions lib/__test__/sentry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getSentry,
getSentryHubWrapper,
} from '../src/sentry';
import { SCRIPT_ERROR } from '../src/constants.js';

jest.mock('@sentry/browser', () => ({
BrowserClient: jest.fn(),
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('Sentry', () => {
const mockHub = {
addBreadcrumb: jest.fn(),
captureMessage: jest.fn(),
captureException: jest.fn(),
captureEvent: jest.fn(),
withScope: jest.fn(callback => callback(mockScope)),
};

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

sentryHubWrapper.captureException('test exception');
expect(mockHub.captureException).toHaveBeenCalledWith('test exception');
sentryHubWrapper.captureException(new Error('test error'));
expect(mockHub.captureEvent).toHaveBeenCalledWith({
message: SCRIPT_ERROR,
level: 'error',
extra: new Error('test error'),
});

sentryHubWrapper.captureException('test non error');
expect(mockHub.captureEvent).toHaveBeenCalledWith({
message: SCRIPT_ERROR,
level: 'error',
extra: 'test non error',
});
});
});

1 change: 1 addition & 0 deletions lib/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
sentry.addBreadcrumb({
category: 'hCaptcha:script',
message: 'hCaptcha failed to load',
data: error,
});

sentry.captureException(error);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ERROR, SCRIPT_ID } from './constants';
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ID } from './constants';
import { getFrame, getMountElement } from './utils';

import type { IScriptParams } from './types';
Expand Down Expand Up @@ -34,7 +34,7 @@ export function fetchScript({
};

script.onload = (event) => onComplete(event, resolve);
script.onerror = (event) => onComplete(event, () => reject(new Error(SCRIPT_ERROR)));
script.onerror = (event) => onComplete(event, reject);

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

Expand Down
10 changes: 7 additions & 3 deletions lib/src/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from '@sentry/browser';

import { ScopeTag, SentryHub } from './types';
import { SCRIPT_ERROR } from './constants';

const SENTRY_DSN = process.env.SENTRY_DSN_TOKEN;

Expand Down Expand Up @@ -71,15 +72,18 @@ export function getSentryHubWrapper(
sentryHub.captureMessage(message);
});
},
captureException: (e) => {
captureException: (error) => {
if (!sentryHub) {
return;
}

sentryHub.withScope(function (scope) {
scope.setTag(tag.key, tag.value);

sentryHub.captureException(e);
sentryHub.captureEvent({
message: SCRIPT_ERROR,
level: 'error',
extra: error
});
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ILoaderParams extends IScriptParams {

export interface SentryHub {
addBreadcrumb: (breadcrumb: object) => void;
captureException: (e: any) => void;
captureException: (error: any) => void;
captureMessage: (message: string) => void;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hcaptcha/loader",
"description": "This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling.",
"version": "1.0.9",
"version": "1.0.10",
"author": "hCaptcha team and contributors",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit d804d14

Please sign in to comment.