Skip to content

Commit

Permalink
MWPW-158652 [MEP] Only output non fatal errors to internal users and …
Browse files Browse the repository at this point in the history
…skip rows missing an action value (#2941)

* Add previewLog

* Add another log

* Add warn

* Remove unecessary log

* Update tests

* Update warn log

* Add code coverage for empty action

* Remove commented code
  • Loading branch information
markpadbe committed Sep 26, 2024
1 parent 05ebf1f commit 4bc2230
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
24 changes: 15 additions & 9 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ const COMMANDS = {
},
};

const log = (...msg) => {
const config = getConfig();
if (config.mep?.preview) console.log(...msg);
};

const fetchData = async (url, type = DATA_TYPE.JSON) => {
try {
const resp = await fetch(normalizePath(url));
Expand All @@ -220,7 +225,7 @@ const fetchData = async (url, type = DATA_TYPE.JSON) => {
return await resp[type]();
} catch (e) {
/* c8 ignore next 3 */
console.log(`Error loading content: ${url}`, e.message || e);
log(`Error loading content: ${url}`, e.message || e);
}
return null;
};
Expand Down Expand Up @@ -319,7 +324,7 @@ const querySelector = (el, selector, all = false) => {
return all ? el.querySelectorAll(selector) : el.querySelector(selector);
} catch (e) {
/* eslint-disable-next-line no-console */
console.log('Invalid selector: ', selector);
log('Invalid selector: ', selector);
return null;
}
};
Expand Down Expand Up @@ -513,6 +518,10 @@ const getVariantInfo = (line, variantNames, variants, manifestPath, fTargetId) =
// retro support
const action = line.action?.toLowerCase()
.replace('content', '').replace('fragment', '').replace('tosection', '');
if (!action) {
log('Row found with empty action field: ', line);
return;
}
const pageFilter = line['page filter'] || line['page filter optional'];
const { selector } = line;

Expand Down Expand Up @@ -565,9 +574,6 @@ const getVariantInfo = (line, variantNames, variants, manifestPath, fTargetId) =
}
} else if (action in COMMANDS || action in CREATE_CMDS) {
variants[vn].commands.push(variantInfo);
} else {
/* c8 ignore next 2 */
console.log('Invalid action found: ', line);
}
});
};
Expand Down Expand Up @@ -599,7 +605,7 @@ export function parseManifestVariants(data, manifestPath, targetId) {
return manifestConfig;
} catch (e) {
/* c8 ignore next 3 */
console.log('error parsing personalization manifestConfig:', e, experiences);
log('error parsing personalization manifestConfig:', e, experiences);
}
return null;
}
Expand Down Expand Up @@ -777,7 +783,7 @@ export async function getManifestConfig(info = {}, variantOverride = false) {

if (!manifestConfig) {
/* c8 ignore next 3 */
console.log('Error loading personalization manifestConfig: ', name || manifestPath);
log('Error loading personalization manifestConfig: ', name || manifestPath);
return null;
}
const infoKeyMap = {
Expand Down Expand Up @@ -915,7 +921,7 @@ export function cleanAndSortManifestList(manifests) {

parsePlaceholders(placeholderData, getConfig(), manifestConfig.selectedVariantName);
} catch (e) {
console.warn(e);
log(`MEP Error parsing manifests: ${e.toString()}`);
window.lana?.log(`MEP Error parsing manifests: ${e.toString()}`);
}
});
Expand Down Expand Up @@ -1071,7 +1077,7 @@ export async function init(enablements = {}) {
try {
await applyPers(manifests, postLCP);
} catch (e) {
console.warn(e);
log(`MEP Error: ${e.toString()}`);
window.lana?.log(`MEP Error: ${e.toString()}`);
}
}
10 changes: 10 additions & 0 deletions test/features/personalization/mepPreviewSettings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mepSettings = {
mepParam: false,
mepHighlight: false,
mepButton: 'on',
pzn: '/path/to/manifest.json',
promo: false,
target: false,
};

export default mepSettings;
19 changes: 19 additions & 0 deletions test/features/personalization/mocks/manifestEmptyAction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"total": 5,
"offset": 0,
"limit": 5,
"data": [
{
"action": "",
"selector": "",
"page filter (optional)": "",
"param-newoffer=123": "",
"chrome": "/test/features/personalization/mocks/myblock",
"target-var1": "/test/features/personalization/mocks/myblock",
"firefox": "",
"android": "",
"ios": ""
}
],
":type": "sheet"
}
20 changes: 17 additions & 3 deletions test/features/personalization/personalization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
init, matchGlob, createContent, combineMepSources, buildVariantInfo,
} from '../../../libs/features/personalization/personalization.js';
import mepSettings from './mepSettings.js';
import mepSettingsPreview from './mepPreviewSettings.js';

document.head.innerHTML = await readFile({ path: './mocks/metadata.html' });
document.body.innerHTML = await readFile({ path: './mocks/personalization.html' });
Expand Down Expand Up @@ -279,14 +280,27 @@ describe('Functional Test', () => {
});
});

it('invalid selector should output error to console', async () => {
it('invalid selector should output error to console in preview mode', async () => {
window.console.log = stub();

await loadManifestAndSetResponse('./mocks/manifestInvalidSelector.json');
await init(mepSettingsPreview);
assert.calledWith(window.console.log, 'Invalid selector: ');
window.console.log.reset();
});

it('invalid selector should not output error to console if not in preview mode', async () => {
window.console.log = stub();
await loadManifestAndSetResponse('./mocks/manifestInvalidSelector.json');
await init(mepSettings);
assert.neverCalledWith(window.console.log, 'Invalid selector: ');
window.console.log.reset();
});

assert.calledWith(window.console.log, 'Invalid selector: ');
it('missing selector should output error to console if in preview mode', async () => {
window.console.log = stub();
await loadManifestAndSetResponse('./mocks/manifestEmptyAction.json');
await init(mepSettingsPreview);
assert.calledWith(window.console.log, 'Row found with empty action field: ');
window.console.log.reset();
});

Expand Down

0 comments on commit 4bc2230

Please sign in to comment.