« get me outta code hell

show item info in errs caught by validateListItems - 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>2022-01-19 23:22:12 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-01-19 23:22:12 -0400
commitca3a698e06bd44346c96f512aebd7fae84d8500b (patch)
tree902be57a7168fd876ce519e60d7b90052155e036
parentcb481dd53f3793c3c12c8e1a41875f024c9cbe11 (diff)
show item info in errs caught by validateListItems
-rw-r--r--src/thing/validators.js23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/thing/validators.js b/src/thing/validators.js
index 0573691..2bdb299 100644
--- a/src/thing/validators.js
+++ b/src/thing/validators.js
@@ -1,5 +1,13 @@
 import { withAggregate } from '../util/sugar.js';
 
+import { color, ENABLE_COLOR } from '../util/cli.js';
+
+import { inspect as nodeInspect } from 'util';
+
+function inspect(value) {
+    return nodeInspect(value, {colors: ENABLE_COLOR});
+}
+
 // Basic types (primitives)
 
 function a(noun) {
@@ -97,12 +105,25 @@ export function isArray(value) {
     return true;
 }
 
+function validateArrayItemsHelper(itemValidator) {
+    return (item, index) => {
+        try {
+            itemValidator(item);
+        } catch (error) {
+            error.message = `(index: ${color.green(index)}, item: ${inspect(item)}) ${error.message}`;
+            throw error;
+        }
+    };
+}
+
 export function validateArrayItems(itemValidator) {
+    const fn = validateArrayItemsHelper(itemValidator);
+
     return array => {
         isArray(array);
 
         withAggregate({message: 'Errors validating array items'}, ({ wrap }) => {
-            array.forEach(wrap(itemValidator));
+            array.forEach(wrap(fn));
         });
 
         return true;