Skip to content

Commit

Permalink
fix(error): pass only errors to captureException (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
faris-imi authored Nov 15, 2023
1 parent 32da8b2 commit fd06af8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/__test__/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, afterAll, beforeAll, describe, it, jest, expect } from '@jes
import { spyOnScriptMethod } from './__mocks__';

import { fetchScript } from "../src/script";
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ID } from "../src/constants";
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ERROR, SCRIPT_ID } from '../src/constants';
import { generateQuery } from '../src/utils';


Expand Down Expand Up @@ -64,17 +64,17 @@ describe('fetchScript', () => {

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

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

expect(spyOnAppend).toHaveBeenCalled();
expect(spyOnRemove).toHaveBeenCalled();
});

it('should reject when internal error is caught', async () => {
const errorInternal = new Error('Invalid Script');
const errorInternal = new Error(SCRIPT_ERROR);

spyOnLoad.set.mockImplementationOnce((callback: (any) => void) => {
callback(new Event('Loaded Script'));
Expand Down
1 change: 1 addition & 0 deletions lib/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function hCaptchaLoader(params: ILoaderParams = { cleanup: true }): Promi
category: 'hCaptcha:script',
message: 'hCaptcha failed to load',
});

sentry.captureException(error);
reject(new Error(SCRIPT_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_ID } from './constants';
import { HCAPTCHA_LOAD_FN_NAME, SCRIPT_ERROR, 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);
script.onerror = (event) => onComplete(event, () => reject(new Error(SCRIPT_ERROR)));

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

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.8",
"version": "1.0.9",
"author": "hCaptcha team and contributors",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit fd06af8

Please sign in to comment.