From 9b52723fe5094cb714e4806704bdf998737d5840 Mon Sep 17 00:00:00 2001 From: Alec Swanson Date: Mon, 23 May 2022 16:31:29 -0700 Subject: [PATCH] fix: handle empty files * Add placeholders for alerts * Add multi-doc parsing for alerts, but only take first * Add placeholders and JSON parse try/catch for dashboards --- src/utils/preview/parseHelpers.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/utils/preview/parseHelpers.js b/src/utils/preview/parseHelpers.js index c2a4f048..323140a9 100644 --- a/src/utils/preview/parseHelpers.js +++ b/src/utils/preview/parseHelpers.js @@ -93,13 +93,16 @@ export const parseDashboardFiles = (files) => { dashFileMetadata.fileName )[0]; - const { name = '', description = '' } = JSON.parse( - dashFileMetadata.content - ); + let dashboard = {}; + try { + dashboard = JSON.parse(dashFileMetadata.content); + } catch (err) { + console.error(err); + } return { - name, - description, + name: dashboard?.name ?? 'Placeholder name', + description: dashboard?.description ?? 'Placeholder description', screenshots: screenshots .filter((s) => s.filePath.includes(parentDir)) .map(({ content }) => content), @@ -114,13 +117,13 @@ export const parseDashboardFiles = (files) => { */ export const parseAlertFiles = (alertFiles) => { return alertFiles.map((file) => { - const loadYaml = yaml.load(file.content); + const loadYaml = yaml.loadAll(file.content)[0]; //parse and build alert object and add it to the array return { - details: loadYaml.description?.trim() ?? '', - name: loadYaml.name?.trim() ?? '', - type: loadYaml.type?.trim() ?? '', + details: loadYaml?.description?.trim() ?? 'Placeholder description', + name: loadYaml?.name?.trim() ?? 'Placeholder name', + type: loadYaml?.type?.trim() ?? 'Placeholder type', }; }); };