« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/data/things/validators.js18
-rw-r--r--test/unit/data/things/validators.js15
2 files changed, 15 insertions, 18 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index 01062e7..c15a6cc 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;
     }
   };
diff --git a/test/unit/data/things/validators.js b/test/unit/data/things/validators.js
index 27d5f4f..178f865 100644
--- a/test/unit/data/things/validators.js
+++ b/test/unit/data/things/validators.js
@@ -115,7 +115,7 @@ test(t, 'isObject', t => {
 });
 
 test(t, 'validateArrayItems', t => {
-  t.plan(6);
+  t.plan(9);
 
   t.ok(validateArrayItems(isNumber)([3, 4, 5]));
   t.ok(validateArrayItems(validateArrayItems(isNumber))([[3, 4], [4, 5], [6, 7]]));
@@ -130,7 +130,10 @@ test(t, 'validateArrayItems', t => {
   t.not(caughtError, null);
   t.ok(caughtError instanceof AggregateError);
   t.equal(caughtError.errors.length, 1);
-  t.ok(caughtError.errors[0] instanceof TypeError);
+  t.ok(caughtError.errors[0] instanceof Error);
+  t.equal(caughtError.errors[0][Symbol.for('hsmusic.annotateError.indexInSourceArray')], 2);
+  t.not(caughtError.errors[0].cause, null);
+  t.ok(caughtError.errors[0].cause instanceof TypeError);
 });
 
 // Wiki data
@@ -267,7 +270,7 @@ test(t, 'validateReferenceList', t => {
   const track = validateReferenceList('track');
   const artist = validateReferenceList('artist');
 
-  t.plan(9);
+  t.plan(11);
 
   t.ok(track(['track:fallen-down', 'Once Upon a Time']));
   t.ok(artist(['artist:toby-fox', 'Mark Hadley']));
@@ -284,8 +287,10 @@ test(t, 'validateReferenceList', t => {
   t.not(caughtError, null);
   t.ok(caughtError instanceof AggregateError);
   t.equal(caughtError.errors.length, 2);
-  t.ok(caughtError.errors[0] instanceof TypeError);
-  t.ok(caughtError.errors[1] instanceof TypeError);
+  t.ok(caughtError.errors[0] instanceof Error);
+  t.ok(caughtError.errors[0].cause instanceof TypeError);
+  t.ok(caughtError.errors[1] instanceof Error);
+  t.ok(caughtError.errors[0].cause instanceof TypeError);
 });
 
 test(t, 'anyOf', t => {