« get me outta code hell

data: validateArrayItems: annotate multiline errors nicely - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-11-20 13:32:10 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-20 13:48:31 -0400
commitee02bc3efebf992c47694ec4065f658473b1f904 (patch)
treeedcd5f27ae8914e70028e828b415236b1d650dd0 /src
parente35d23f4e9492b497138dce3f21382872e329e71 (diff)
data: validateArrayItems: annotate multiline errors nicely
Diffstat (limited to 'src')
-rw-r--r--src/data/things/validators.js17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index f60c363c..e213e933 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -1,5 +1,9 @@
 import {inspect as nodeInspect} from 'node:util';
 
+// Heresy.
+import printable_characters from 'printable-characters';
+const {strlen} = printable_characters;
+
 import {colors, ENABLE_COLOR} from '#cli';
 import {empty, typeAppearance, withAggregate} from '#sugar';
 
@@ -174,8 +178,19 @@ function validateArrayItemsHelper(itemValidator) {
         throw new Error(`Expected validator to return true`);
       }
     } catch (error) {
-      error.message = `(index: ${colors.yellow(`${index}`)}, item: ${inspect(item)}) ${error.message}`;
+      const annotation = `(index: ${colors.yellow(`${index}`)}, item: ${inspect(item)})`;
+
+      error.message =
+        (error.message.includes('\n') || strlen(annotation) > 20
+          ? annotation + '\n' +
+            error.message
+              .split('\n')
+              .map(line => `  ${line}`)
+              .join('\n')
+          : `${annotation} ${error}`);
+
       error[Symbol.for('hsmusic.decorate.indexInSourceArray')] = index;
+
       throw error;
     }
   };