Send http 404 when algorithm not found

This commit is contained in:
Jason Park
2019-01-24 02:59:56 -05:00
parent f025e8827c
commit 0ffee2e5dc
2 changed files with 14 additions and 5 deletions

View File

@@ -56,16 +56,22 @@ app.use((req, res) => {
const [, categoryKey, algorithmKey] = url.parse(req.originalUrl).pathname.split('/');
let { title, description } = packageJson;
const algorithm = hierarchy.find(categoryKey, algorithmKey);
if (algorithm) {
title = [algorithm.categoryName, algorithm.algorithmName].join(' - ');
description = algorithm.description;
let algorithm = undefined;
if (categoryKey && categoryKey !== 'scratch-paper') {
algorithm = hierarchy.find(categoryKey, algorithmKey) || null;
if (algorithm) {
title = [algorithm.categoryName, algorithm.algorithmName].join(' - ');
description = algorithm.description;
} else {
res.status(404);
}
}
const indexFile = res.indexFile
.replace(/\$TITLE/g, title)
.replace(/\$DESCRIPTION/g, description)
.replace(/\$ALGORITHM/g, algorithm ? JSON.stringify(algorithm).replace(/</g, '\\u003c') : 'undefined');
.replace(/\$ALGORITHM/g, algorithm === undefined ? 'undefined' :
JSON.stringify(algorithm).replace(/</g, '\\u003c'));
res.send(indexFile);
});

View File

@@ -148,6 +148,9 @@ class App extends BaseComponent {
if (window.__PRELOADED_ALGORITHM__) {
this.props.setAlgorithm(window.__PRELOADED_ALGORITHM__);
delete window.__PRELOADED_ALGORITHM__;
} else if (window.__PRELOADED_ALGORITHM__ === null) {
delete window.__PRELOADED_ALGORITHM__;
return Promise.reject(new Error('Algorithm Not Found'));
} else if (categoryKey && algorithmKey) {
return AlgorithmApi.getAlgorithm(categoryKey, algorithmKey)
.then(({ algorithm }) => this.props.setAlgorithm(algorithm));