Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loader): add retry logic #12

Merged
merged 8 commits into from
Dec 4, 2023
Merged

feat(loader): add retry logic #12

merged 8 commits into from
Dec 4, 2023

Conversation

faris-imi
Copy link
Contributor

No description provided.

@faris-imi faris-imi requested a review from brdlyptrs November 10, 2023 17:45
@faris-imi faris-imi self-assigned this Nov 10, 2023
@faris-imi faris-imi requested a review from a team as a code owner November 10, 2023 17:45
lib/src/loader.ts Outdated Show resolved Hide resolved
@hCaptcha hCaptcha deleted a comment from LeQuangHuy-1809 Nov 16, 2023
@brdlyptrs
Copy link
Contributor

@faris-imi can you add some test to this.


const sentry = initSentry(params.sentry);

return hCaptchaApi(params, sentry).catch(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brdlyptrs rejection is not being caught by try/catch, used .catch() instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats weird, catch should be the same whether this way or vis try / catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was due to not awaiting hCaptchaApi before returning function, fixed and reverted try/catch

message: 'hCaptcha loaded',
});

hCaptchaScripts.push({ promise, scope: frame.window });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brdlyptrs moved this to resolve block in order to exclude rejected promises from hCaptchaScripts

});

it('should try loading 2 times and succeed on final try', async () => {
mockFetchScript.mockRejectedValueOnce(SCRIPT_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mockFetchScript.mockRejectedValueOnce(SCRIPT_ERROR);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Is it queuing these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, they are queued (latest mock is being called first)
If we remove one rejection mock, it will reject only once (fetchScript will be called twice) meaning test will fail

@brdlyptrs
Copy link
Contributor

brdlyptrs commented Dec 2, 2023

Let's update the README description to include the retry logic as well.

This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling. It also includes a retry mechanism that will attempt to load the hCaptcha script several times in the event if fails to load due to a network or unforeseen issue.

Copy link

@zoryana94 zoryana94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The retry mechanism is working 👍

export const SENTRY_TAG = '@hCaptcha/loader';

export const RETRY_COUNT = 2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAX_RETRIES may sound a bit more descriptive here

return promise;
} catch (error) {
sentry.captureException(error);
return Promise.reject(new Error(SCRIPT_ERROR));
}
}

export async function loadScript(params, retries = 0) {
const message = retries < RETRY_COUNT ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can create the variable areRetriesExceeded and use it in the if below

@@ -1,6 +1,6 @@
# hCaptcha Loader

This is a JavaScript library to easily configure the loading of the [hCaptcha](https://www.hcaptcha.com) JS client SDK with built-in error handling.
This is a JavaScript library to easily configure the loading of the [hCaptcha](https://www.hcaptcha.com) JS client SDK with built-in error handling. It also includes a retry mechanism that will attempt to load the hCaptcha script several times in the event if fails to load due to a network or unforeseen issue.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also includes a retry mechanism that will attempt to load the hCaptcha script if it fails to load due to a network or unforeseen issue.

zoryana94
zoryana94 previously approved these changes Dec 4, 2023
@faris-imi faris-imi merged commit ead10d6 into main Dec 4, 2023
3 checks passed
@faris-imi faris-imi deleted the feat/retry-loader branch December 4, 2023 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants