Skip to content

Commit 59e986c

Browse files
authored
Merge pull request #7638 from plotly/cam/7637/catch-error-malformed-json
fix: Log error message if malformed JSON file found
2 parents 350e247 + a0f9469 commit 59e986c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

devtools/dashboard_utilities.mjs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,28 @@ export function createMocksList(files) {
4040
const jsonFiles = files.filter((file) => file.name.substr(-5) === '.json');
4141

4242
const mocksList = jsonFiles.map((file) => {
43-
const contents = JSON.parse(file.contents);
43+
try {
44+
const contents = JSON.parse(file.contents);
4445

45-
// get plot type keywords from mocks
46-
const types = contents.data
47-
.map((trace) => trace.type || 'scatter')
48-
.reduce((acc, type, i, arr) => (arr.lastIndexOf(type) === i ? [...acc, type] : acc), []);
46+
// get plot type keywords from mocks
47+
const types = contents.data
48+
.map((trace) => trace.type || 'scatter')
49+
.reduce((acc, type, i, arr) => (arr.lastIndexOf(type) === i ? [...acc, type] : acc), []);
4950

50-
const filename = file.name.split(path.sep).pop();
51+
const filename = file.name.split(path.sep).pop();
5152

52-
return {
53-
name: filename.slice(0, -5),
54-
file: filename,
55-
keywords: types.join(', ')
56-
};
53+
return {
54+
name: filename.slice(0, -5),
55+
file: filename,
56+
keywords: types.join(', ')
57+
};
58+
} catch (error) {
59+
if (error instanceof SyntaxError) {
60+
console.log(`Couldn't parse ${file.name} as JSON. Excluding from mocks list.`);
61+
} else {
62+
throw error;
63+
}
64+
}
5765
});
5866

5967
return mocksList;

0 commit comments

Comments
 (0)