« 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
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
parente35d23f4e9492b497138dce3f21382872e329e71 (diff)
data: validateArrayItems: annotate multiline errors nicely
-rw-r--r--package-lock.json14
-rw-r--r--package.json1
-rw-r--r--src/data/things/validators.js17
3 files changed, 31 insertions, 1 deletions
diff --git a/package-lock.json b/package-lock.json
index 9e8f4dfa..bca46bd5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -16,6 +16,7 @@
                 "image-size": "^1.0.2",
                 "js-yaml": "^4.1.0",
                 "marked": "^10.0.0",
+                "printable-characters": "^1.0.42",
                 "striptags": "^4.0.0-alpha.4",
                 "word-wrap": "^1.2.3"
             },
@@ -26,6 +27,9 @@
                 "chokidar": "^3.5.3",
                 "tap": "^18.4.0",
                 "tcompare": "^6.0.0"
+            },
+            "engines": {
+                "node": ">= 20.9.0"
             }
         },
         "node_modules/@alcalzone/ansi-tokenize": {
@@ -3954,6 +3958,11 @@
                 "node": ">= 0.8.0"
             }
         },
+        "node_modules/printable-characters": {
+            "version": "1.0.42",
+            "resolved": "https://registry.npmjs.org/printable-characters/-/printable-characters-1.0.42.tgz",
+            "integrity": "sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ=="
+        },
         "node_modules/prismjs": {
             "version": "1.29.0",
             "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.29.0.tgz",
@@ -8576,6 +8585,11 @@
             "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
             "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g=="
         },
+        "printable-characters": {
+            "version": "1.0.42",
+            "resolved": "https://registry.npmjs.org/printable-characters/-/printable-characters-1.0.42.tgz",
+            "integrity": "sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ=="
+        },
         "prismjs": {
             "version": "1.29.0",
             "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.29.0.tgz",
diff --git a/package.json b/package.json
index 719a5b89..b7467fd4 100644
--- a/package.json
+++ b/package.json
@@ -53,6 +53,7 @@
         "image-size": "^1.0.2",
         "js-yaml": "^4.1.0",
         "marked": "^10.0.0",
+        "printable-characters": "^1.0.42",
         "striptags": "^4.0.0-alpha.4",
         "word-wrap": "^1.2.3"
     },
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;
     }
   };