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

MWPW-131664: increase merch resilience to errors #765

Merged
merged 5 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions libs/blocks/merch/merch.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ window.tacocat.loadPromise = new Promise((resolve) => {
.then(() => loadScript(scriptUrl))
.then(() => {
runTacocat(tacocatEnv, country, language);
resolve();
resolve(false);
})
.catch((error) => {
console.error('Failed to load tacocat', error);
resolve(true);
});
});

Expand Down Expand Up @@ -161,10 +165,8 @@ function getCheckoutContext(searchParams, config) {

export default async function init(el) {
if (!el?.classList?.contains('merch')) return undefined;
try {
await window.tacocat.loadPromise;
} catch (e) {
console.error('Tacocat not loaded', e);
const fail = await window.tacocat.loadPromise;
if (fail) {
return undefined;
}
const { searchParams } = new URL(el.href);
Expand Down
4 changes: 2 additions & 2 deletions test/blocks/merch/merch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ document.body.innerHTML = await readFile({ path: './mocks/body.html' });
describe('Merch Block', () => {
before(async () => {
Object.assign(window.tacocat, {
loadPromise: Promise.resolve(),
loadPromise: Promise.resolve(false),
price: { optionProviders: [] },
defaults: {
apiKey: 'wcms-commerce-ims-ro-user-milo',
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('Merch Block', () => {
});

it('does not initialize the block when tacocat fails to load', async () => {
window.tacocat.loadPromise = Promise.reject(new Error('404'));
window.tacocat.loadPromise = Promise.resolve(true);
let el = document.querySelector('.merch.cta.notacocat');
el = await merch(el);
expect(el).to.be.undefined;
Expand Down