« get me outta code hell

validators: validateArrayItems: nest in a cause layer - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-03 20:32:23 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-03 20:35:42 -0400
commit9e20be3397fde456ae770cc7d4dfe95cfc2eff3f (patch)
tree0f077e54aa445f50fd9f9bf79aac915ca29ea58a /src/data/things
parent0c5a8f4a648d25fd06d753226dac5da7605f35b3 (diff)
validators: validateArrayItems: nest in a cause layer
Diffstat (limited to 'src/data/things')
-rw-r--r--src/data/things/validators.js18
1 files changed, 5 insertions, 13 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index 01062e7d..c15a6cc7 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -213,20 +213,12 @@ function validateArrayItemsHelper(itemValidator) {
       if (value !== true) {
         throw new Error(`Expected validator to return true`);
       }
-    } catch (error) {
-      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}`);
-
+    } catch (caughtError) {
+      const indexPart = colors.yellow(`zero-index ${index}`)
+      const itemPart = inspect(item);
+      const message = `Error at ${indexPart}: ${itemPart}`;
+      const error = new Error(message, {cause: caughtError});
       error[Symbol.for('hsmusic.annotateError.indexInSourceArray')] = index;
-
       throw error;
     }
   };