« get me outta code hell

sugar: separate annotateError functions & utilities - 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-06 15:34:52 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-06 15:34:52 -0400
commit32f5dfa4b3c12dec18d0655160f9d49ca93b16d9 (patch)
treed6f6331410ffaf46af43e031e27e66d886f6e664
parent9f37683838ffc9f04b4e705d382a101b0b422412 (diff)
sugar: separate annotateError functions & utilities
-rw-r--r--src/util/sugar.js34
-rw-r--r--test/unit/data/composite/data/withPropertiesFromObject.js4
2 files changed, 34 insertions, 4 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 6718b697..d5f0fbd3 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -637,13 +637,31 @@ export function showAggregate(topError, {
   }
 }
 
+export function annotateError(error, ...callbacks) {
+  for (const callback of callbacks) {
+    error = callback(error) ?? error;
+  }
+
+  return error;
+}
+
+export function annotateErrorWithIndex(error, index) {
+  return Object.assign(error, {
+    [Symbol.for('hsmusic.annotateError.indexInSourceArray')]:
+      index,
+
+    message:
+      `(${colors.yellow(`#${index + 1}`)}) ` +
+      error.message,
+  });
+}
+
 export function decorateErrorWithIndex(fn) {
   return (x, index, array) => {
     try {
       return fn(x, index, array);
     } catch (error) {
-      error.message = `(${colors.yellow(`#${index + 1}`)}) ${error.message}`;
-      error[Symbol.for('hsmusic.decorate.indexInSourceArray')] = index;
+      annotateErrorWithIndex(error, index);
       throw error;
     }
   };
@@ -660,6 +678,18 @@ export function decorateErrorWithCause(fn, cause) {
   };
 }
 
+export function annotateErrorWithFile(error, file) {
+  return Object.assign(error, {
+    [Symbol.for('hsmusic.annotateError.file')]:
+      file,
+
+    message:
+      error.message +
+      (error.message.includes('\n') ? '\n' : ' ') +
+      `(file: ${colors.bright(colors.blue(file))})`,
+  });
+}
+
 export function conditionallySuppressError(conditionFn, callbackFn) {
   return (...args) => {
     try {
diff --git a/test/unit/data/composite/data/withPropertiesFromObject.js b/test/unit/data/composite/data/withPropertiesFromObject.js
index ead1b9b2..cb1d8d21 100644
--- a/test/unit/data/composite/data/withPropertiesFromObject.js
+++ b/test/unit/data/composite/data/withPropertiesFromObject.js
@@ -207,7 +207,7 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => {
       {message: `object: Expected an object, got array`},
       {message: `properties: Errors validating array items`, errors: [
         {
-          [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2,
+          [Symbol.for('hsmusic.annotateError.indexInSourceArray')]: 2,
           message: /Expected a string, got number/,
         },
       ]},
@@ -240,7 +240,7 @@ t.test(`withPropertiesFromObject: validate dynamic inputs`, t => {
           {message: `object: Expected an object, got array`},
           {message: `properties: Errors validating array items`, errors: [
             {
-              [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2,
+              [Symbol.for('hsmusic.annotateError.indexInSourceArray')]: 2,
               message: /Expected a string, got number/,
             },
           ]},