Skip to content

Commit

Permalink
throw 404 when dataset is not available, compatibility fallback for o…
Browse files Browse the repository at this point in the history
…ld narratives
  • Loading branch information
salvatore-fxpig committed Jun 12, 2020
1 parent d1cdf40 commit 6c322d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cli/server/getDataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const setUpGetDatasetHandler = ({datasetsPath}) => {
await helpers.sendJson(res, info);
} catch (err) {
console.trace(err);
return helpers.handleError(res, `couldn't fetch JSONs`, err.message);
// Throw 404 when not available
const errorCode = err.message.endsWith("not in available datasets") ? 404 : 500;
return helpers.handleError(res, `couldn't fetch JSONs`, err.message, errorCode);
}
};
};
Expand Down
4 changes: 2 additions & 2 deletions cli/server/getDatasetHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const path = require("path");
const convertFromV1 = require("./convertJsonSchemas").convertFromV1;
const fs = require("fs");

const handleError = (res, clientMsg, serverMsg="") => {
const handleError = (res, clientMsg, serverMsg="", code = 500) => {
res.statusMessage = clientMsg;
utils.warn(`${clientMsg} -- ${serverMsg}`);
return res.status(500).end();
return res.status(code).end();
};

const splitPrefixIntoParts = (url) => url
Expand Down
31 changes: 26 additions & 5 deletions src/actions/loadData.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const getDatasetFromCharon = (prefix, {type, narrative=false}={}) => {
if (type) path += `&type=${type}`;
const p = fetch(path)
.then((res) => {
//throw the whole thing so we can check res.status
if (res.status !== 200) {
throw new Error(res.statusText);
throw res;
}
return res;
});
Expand All @@ -55,8 +56,9 @@ const getHardcodedData = (prefix, {type="mainJSON"}={}) => {

const p = fetch(datapaths[type])
.then((res) => {
//throw the whole thing so we can check res.status
if (res.status !== 200) {
throw new Error(res.statusText);
throw res;
}
return res;
});
Expand Down Expand Up @@ -265,12 +267,31 @@ const fetchAndCacheNarrativeDatasets = async (dispatch, blocks, query) => {
// 2. allow loading dataset for secondTreeName

// We block and await for the landing dataset
jsons[startingTreeName] = landingSlide.json = await getDataset(startingTreeName).then(((res) => res.json()));
jsons[startingTreeName] = landingSlide.json = await
getDataset(startingTreeName)
.then(res => res.json())
// If it's a 404 we fall back
.catch((err) => {
if (err.status === 404) {
// Assuming block[0] is the one that was set properly for all legacy narratives
return getDataset(treeNames[0]);
}
throw err;
});

// The other datasets are fetched asynchronously
for (const treeName of treeNames)
// With this there's no need for Set above
jsons[treeName] = jsons[treeName] || getDataset(treeName).then((res) => res.json());
// With this there's no need for Set above
jsons[treeName] = jsons[treeName] ||
getDataset(treeName)
.then((res) => res.json())
.catch((err) => {
if (err.status === 404) {
// We fall back to the landing slide
return jsons[startingTreeName];
}
throw err;
});;

// I don't think the below here is a real problem for any practical case (?)

Expand Down

0 comments on commit 6c322d9

Please sign in to comment.