Skip to content

Commit

Permalink
Add try catch around Storage API access
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Jul 8, 2024
1 parent 7dfabd7 commit 398c372
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
16 changes: 12 additions & 4 deletions blocks/product-recommendations/product-recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,25 @@ async function loadRecommendation(block, context, visibility, filters) {
const viewHistory = window.localStorage.getItem(`${storeViewCode}:productViewHistory`) || '[]';
context.userViewHistory = JSON.parse(viewHistory);
} catch (e) {
window.localStorage.removeItem('productViewHistory');
console.error('Error parsing product view history', e);
try {
window.localStorage.removeItem('productViewHistory');
} catch (f) {
// Do nothing
}
console.warn('Error parsing product view history', e);

Check warning on line 166 in blocks/product-recommendations/product-recommendations.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}

// Get purchase history
try {
const purchaseHistory = window.localStorage.getItem(`${storeViewCode}:purchaseHistory`) || '[]';
context.userPurchaseHistory = JSON.parse(purchaseHistory);
} catch (e) {
window.localStorage.removeItem('purchaseHistory');
console.error('Error parsing purchase history', e);
try {
window.localStorage.removeItem('purchaseHistory');
} catch (f) {
// Do nothing
}
console.warn('Error parsing purchase history', e);

Check warning on line 179 in blocks/product-recommendations/product-recommendations.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}

window.adobeDataLayer.push((dl) => {
Expand Down
31 changes: 25 additions & 6 deletions scripts/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export const calcEnvironment = () => {
if (href.includes('localhost')) {
environment = 'dev';
}

const environmentFromConfig = window.sessionStorage.getItem('environment');
if (environmentFromConfig && ALLOWED_CONFIGS.includes(environmentFromConfig) && environment !== 'prod') {
return environmentFromConfig;
try {
const environmentFromConfig = window.sessionStorage.getItem('environment');
if (environmentFromConfig && ALLOWED_CONFIGS.includes(environmentFromConfig) && environment !== 'prod') {
return environmentFromConfig;
}
} catch (e) {
// Do nothing
}

return environment;
Expand All @@ -37,15 +40,31 @@ function buildConfigURL(environment) {

const getConfigForEnvironment = async (environment) => {
const env = environment || calcEnvironment();
let configJSON = window.sessionStorage.getItem(`config:${env}`);
let configJSON;

try {
configJSON = window.sessionStorage.getItem(`config:${env}`);
if (!configJSON) {
throw new Error('No config in sessionStorage');
}
} catch (e) {
// Do nothing
}

if (!configJSON) {
configJSON = await fetch(buildConfigURL(env));
if (!configJSON.ok) {
throw new Error(`Failed to fetch config for ${env}`);
}
configJSON = await configJSON.text();
window.sessionStorage.setItem(`config:${env}`, configJSON);

try {
window.sessionStorage.setItem(`config:${env}`, configJSON);
} catch (e) {
// Do nothing
}
}

return configJSON;
};

Expand Down
21 changes: 17 additions & 4 deletions scripts/minicart/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class Store {
};

static getCartId() {
const cartIdField = window.localStorage.getItem(Store.CARTID_STORE);
let cartIdField;
try {
cartIdField = window.localStorage.getItem(Store.CARTID_STORE);
} catch (e) {
// Do nothing
}
if (!cartIdField) {
return null;
}
Expand Down Expand Up @@ -91,14 +96,22 @@ class Store {
const parsed = JSON.parse(window.localStorage.getItem(`${this.key}_${this.cartId}`)) || Store.DEFAULT_CART;
return parsed;
} catch (err) {
console.error('Failed to parse cart from localStore. Resetting it.');
window.localStorage.removeItem(`${this.key}_${this.cartId}`);
console.warn('Failed to parse cart from localStore. Resetting it.');
try {
window.localStorage.removeItem(`${this.key}_${this.cartId}`);
} catch (e) {
// Do nothing
}
}
return Store.DEFAULT_CART;
}

resetCart() {
window.localStorage.removeItem(`${this.key}_${this.cartId}`);
try {
window.localStorage.removeItem(`${this.key}_${this.cartId}`);
} catch (e) {
// Do nothing
}
this.cartId = null;
}

Expand Down
8 changes: 6 additions & 2 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,12 @@ async function loadLazy(doc) {
import('./acdl/adobe-client-data-layer.min.js'),
]);

if (sessionStorage.getItem('acdl:debug')) {
import('./acdl/validate.js');
try {
if (sessionStorage.getItem('acdl:debug')) {
import('./acdl/validate.js');
}
} catch (e) {
// do nothing
}

trackHistory();
Expand Down

0 comments on commit 398c372

Please sign in to comment.