Skip to content

Commit 8589017

Browse files
authored
Merge pull request #17 from mcclatchy/config-reject-handler
rejecting config.load on error
2 parents a66bb1b + f407314 commit 8589017

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

demo/story/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import distributeZones from "/index.js";
2020

2121
// Mimic the Yozons implementation
22-
distributeZones(locker);
22+
distributeZones(locker)
23+
.then(() => { console.log("zones: clean render") })
24+
.catch((e) => { console.warn(e) });
2325

2426
// Listen for the loaded event
2527
window.addEventListener("zones-loaded", console.log("zones are loaded"));

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ function distributeZones(locker) {
2626
return new Promise((resolve, reject) => {
2727
locker.executeWhenDOMReady(async () => {
2828
// Config keys match pageType coming from Yozons
29-
const file = locker.config[locker.pageType];
29+
const match = locker.config[locker.pageType];
3030

3131
// Load config file
32-
if(file) {
33-
await config.load(file);
32+
if(match) {
33+
try {
34+
await config.load(match);
35+
} catch(e) {
36+
reject(e);
37+
}
3438
} else {
35-
reject("not a matching page type");
39+
reject("zones: not a matching page type");
3640
}
3741

3842
// Temporary cleanup

lib/config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,24 @@ import { locker, Zone, ignore, distribute } from "./zones.js";
1212

1313
export function load(url) {
1414
return new Promise(async (resolve, reject) => {
15+
let data;
1516
const res = await fetch(url);
16-
const data = await res.json();
17+
18+
// Reject if file missing
19+
if(!res.ok) {
20+
reject("zones: missing config file");
21+
return;
22+
}
23+
24+
// Reject if file is not JSON
25+
try {
26+
data = await res.json();
27+
} catch(e) {
28+
reject("zones: config file not JSON");
29+
return;
30+
}
31+
32+
// Configs
1733
const template = locker.pageType;
1834
const subscriber = locker.user.isSubscriber();
1935
const dma = subscriber ? true : await locker.user.isInDMA();

0 commit comments

Comments
 (0)