« get me outta code hell

sugar: refactor determineCause, determineHelpers - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-05 22:00:34 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-06 12:50:30 -0400
commitb5386dea10c122ac1a71d82a77014cf37f8c11db (patch)
tree1f4712428d29acc9bbecd788a2b151ff668ff042 /src/util
parent48ae5892015d60a1b0a353c07bdb919320bbaae3 (diff)
sugar: refactor determineCause, determineHelpers
Diffstat (limited to 'src/util')
-rw-r--r--src/util/sugar.js55
1 files changed, 33 insertions, 22 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index a447e6e..26982eb 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -633,43 +633,54 @@ export function showAggregate(topError, {
   showTranslucent = showTraces,
   print = true,
 } = {}) {
-  const translucentSymbol = Symbol.for('hsmusic.aggregate.translucent');
+  const getTranslucency = error =>
+    error[Symbol.for('hsmusic.aggregate.translucent')] ?? false;
 
-  const determineCause = error => {
-    let cause = error.cause;
-    if (showTranslucent) return cause ?? null;
+  const determineCauseHelper = cause => {
+    if (!cause) {
+      return null;
+    }
+
+    const translucency = getTranslucency(cause);
 
-    while (cause) {
-      if (!cause[translucentSymbol]) return cause;
-      cause = cause.cause;
+    if (!translucency) {
+      return cause;
     }
 
-    return null;
+
+    return determineCauseHelper(cause.cause);
   };
 
-  const determineErrors = parentError => {
-    if (!parentError.errors) return null;
-    if (showTranslucent) return parentError.errors;
+  const determineCause = error =>
+    (showTranslucent
+      ? error.cause ?? null
+      : determineCauseHelper(error.cause));
+
+  const determineErrorsHelper = error => {
+    const translucency = getTranslucency(error);
+
+    if (!translucency) {
+      return [error];
+    }
 
     const errors = [];
-    for (const error of parentError.errors) {
-      if (!error[translucentSymbol]) {
-        errors.push(error);
-        continue;
-      }
 
-      if (error.cause) {
-        errors.push(determineCause(error));
-      }
+    if (error.cause) {
+      errors.push(...determineErrorsHelper(error.cause));
+    }
 
-      if (error.errors) {
-        errors.push(...determineErrors(error));
-      }
+    if (error.errors) {
+      errors.push(...error.errors.flatMap(determineErrorsHelper));
     }
 
     return errors;
   };
 
+  const determineErrors = error =>
+    (showTranslucent
+      ? error.errors ?? null
+      : error.errors?.flatMap(determineErrorsHelper) ?? null);
+
   const flattenErrorStructure = (error, level = 0) => {
     const cause = determineCause(error);
     const errors = determineErrors(error);