« get me outta code hell

Merge pull request #138 from hsmusic/empty-documents - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-01-15 20:28:46 -0400
committerGitHub <noreply@github.com>2023-01-15 20:28:46 -0400
commit088a0d4ef42c90d6e81586601ee6cb37340b5bf4 (patch)
tree45da1890b44b751e4c38cf342181ab8877ff319a
parent09d909cad62d325acdce46710e188853e3ef608a (diff)
parent2a31d5dcd91fc3f5ee0cd461a7ead628131318b1 (diff)
Merge pull request #138 from hsmusic/empty-documents
Be more resilient in handling empty YAML documents
-rw-r--r--src/data/yaml.js54
1 files changed, 27 insertions, 27 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 02e56d64..b00d68ee 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -968,7 +968,7 @@ export async function loadAndProcessDataDocuments({dataPath}) {
             });
           } else {
             const {result, aggregate} = mapAggregate(
-              yamlResult,
+              yamlResult.filter(Boolean),
               decorateErrorWithIndex(dataStep.processDocument),
               {message: `Errors processing documents`});
             processResults = result;
@@ -994,7 +994,7 @@ export async function loadAndProcessDataDocuments({dataPath}) {
           typeof dataStep.files === 'function'
             ? await callAsync(dataStep.files, dataPath)
             : dataStep.files
-        )
+        );
 
         if (!files) {
           return;
@@ -1005,8 +1005,7 @@ export async function loadAndProcessDataDocuments({dataPath}) {
         const readResults = await mapAsync(
           files,
           (file) => readFile(file, 'utf-8').then((contents) => ({file, contents})),
-          {message: `Errors reading data files`}
-        );
+          {message: `Errors reading data files`});
 
         const yamlResults = map(
           readResults,
@@ -1014,8 +1013,7 @@ export async function loadAndProcessDataDocuments({dataPath}) {
             file,
             documents: yaml.loadAll(contents),
           })),
-          {message: `Errors parsing data files as valid YAML`}
-        );
+          {message: `Errors parsing data files as valid YAML`});
 
         let processResults;
 
@@ -1026,12 +1024,17 @@ export async function loadAndProcessDataDocuments({dataPath}) {
             yamlResults.forEach(({file, documents}) => {
               const [headerDocument, ...entryDocuments] = documents;
 
+              if (!headerDocument) {
+                call(decorateErrorWithFile(() => {
+                  throw new Error(`Missing header document (empty file or erroneously starting with "---"?)`);
+                }), {file});
+                return;
+              }
+
               const header = call(
                 decorateErrorWithFile(({document}) =>
-                  dataStep.processHeaderDocument(document)
-                ),
-                {file, document: headerDocument}
-              );
+                  dataStep.processHeaderDocument(document)),
+                {file, document: headerDocument});
 
               // Don't continue processing files whose header
               // document is invalid - the entire file is excempt
@@ -1041,14 +1044,13 @@ export async function loadAndProcessDataDocuments({dataPath}) {
               }
 
               const entries = map(
-                entryDocuments.map((document) => ({file, document})),
+                entryDocuments
+                  .filter(Boolean)
+                  .map((document) => ({file, document})),
                 decorateErrorWithFile(
                   decorateErrorWithIndex(({document}) =>
-                    dataStep.processEntryDocument(document)
-                  )
-                ),
-                {message: `Errors processing entry documents`}
-              );
+                    dataStep.processEntryDocument(document))),
+                {message: `Errors processing entry documents`});
 
               // Entries may be incomplete (i.e. any errored
               // documents won't have a processed output
@@ -1066,22 +1068,20 @@ export async function loadAndProcessDataDocuments({dataPath}) {
 
             yamlResults.forEach(({file, documents}) => {
               if (documents.length > 1) {
-                call(
-                  decorateErrorWithFile(() => {
-                    throw new Error(
-                      `Only expected one document to be present per file`
-                    );
-                  })
-                );
+                call(decorateErrorWithFile(() => {
+                  throw new Error(`Only expected one document to be present per file`);
+                }), {file});
                 return;
+              } else if (empty(documents) || !documents[0]) {
+                call(decorateErrorWithFile(() => {
+                  throw new Error(`Expected a document, this file is empty`);
+                }), {file});
               }
 
               const result = call(
                 decorateErrorWithFile(({document}) =>
-                  dataStep.processDocument(document)
-                ),
-                {file, document: documents[0]}
-              );
+                  dataStep.processDocument(document)),
+                {file, document: documents[0]});
 
               if (!result) {
                 return;