« get me outta code hell

sugar: revamp showAggregate helpful trace line algorithm - 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-12-30 09:51:40 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 13:07:03 -0400
commit6f1642dd0f1fc79b05b98fb892d6258cfffc7e15 (patch)
treef9a6d48827ba1d6a66c41526d67553331f5d0993
parentaa4a8d9a3170a6674f2ec4d739743cc9a1e9bf4a (diff)
sugar: revamp showAggregate helpful trace line algorithm
-rw-r--r--src/util/sugar.js89
1 files changed, 69 insertions, 20 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index a6d50943..faf685b5 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -589,30 +589,42 @@ export function _withAggregate(mode, aggregateOpts, fn) {
   }
 }
 
-export const unhelpfulStackLines = [
+export const unhelpfulTraceLines = [
   /sugar/,
   /node:/,
   /<anonymous>/,
 ];
 
-export function getUsefulStackLine(stack) {
-  if (!stack) return '';
+export function getUsefulTraceLine(trace, {helpful, unhelpful}) {
+  if (!trace) return '';
 
-  function isUseful(stackLine) {
-    const trimmed = stackLine.trim();
+  for (const traceLine of trace.split('\n')) {
+    if (!traceLine.trim().startsWith('at')) {
+      continue;
+    }
+
+    if (!empty(unhelpful)) {
+      if (unhelpful.some(regex => regex.test(traceLine))) {
+        continue;
+      }
+    }
 
-    if (!trimmed.startsWith('at'))
-      return false;
+    if (!empty(helpful)) {
+      for (const regex of helpful) {
+        const match = traceLine.match(regex);
 
-    if (unhelpfulStackLines.some(regex => regex.test(trimmed)))
-      return false;
+        if (match) {
+          return match[1] ?? traceLine;
+        }
+      }
 
-    return true;
+      continue;
+    }
+
+    return traceLine;
   }
 
-  const stackLines = stack.split('\n');
-  const usefulStackLine = stackLines.find(isUseful);
-  return usefulStackLine ?? '';
+  return '';
 }
 
 export function showAggregate(topError, {
@@ -667,7 +679,11 @@ export function showAggregate(topError, {
 
       kind: error.constructor.name,
       message: error.message,
-      stack: error.stack,
+
+      trace:
+        (error[Symbol.for(`hsmusic.aggregate.traceFrom`)]
+          ? error[Symbol.for(`hsmusic.aggregate.traceFrom`)].stack
+          : error.stack),
 
       cause:
         (cause
@@ -678,10 +694,33 @@ export function showAggregate(topError, {
         (errors
           ? errors.map(error => flattenErrorStructure(error, level + 1))
           : null),
+
+      options: {
+        alwaysTrace:
+          error[Symbol.for(`hsmusic.aggregate.alwaysTrace`)],
+
+        helpfulTraceLines:
+          error[Symbol.for(`hsmusic.aggregate.helpfulTraceLines`)],
+
+        unhelpfulTraceLines:
+          error[Symbol.for(`hsmusic.aggregate.unhelpfulTraceLines`)],
+      }
     };
   };
 
-  const recursive = ({level, kind, message, stack, cause, errors}) => {
+  const recursive = ({
+    level,
+    kind,
+    message,
+    trace,
+    cause,
+    errors,
+    options: {
+      alwaysTrace,
+      helpfulTraceLines: ownHelpfulTraceLines,
+      unhelpfulTraceLines: ownUnhelpfulTraceLines,
+    },
+  }) => {
     const messagePart =
       message || `(no message)`;
 
@@ -695,14 +734,24 @@ export function showAggregate(topError, {
         ? `[${messagePart}]`
         : messagePart);
 
-    if (showTraces) {
-      const stackLine =
-        getUsefulStackLine(stack);
+    if (showTraces || alwaysTrace) {
+      const traceLine =
+        getUsefulTraceLine(trace, {
+          unhelpful:
+            (ownUnhelpfulTraceLines
+              ? unhelpfulTraceLines.concat(ownUnhelpfulTraceLines)
+              : unhelpfulTraceLines),
+
+          helpful:
+            (ownHelpfulTraceLines
+              ? ownHelpfulTraceLines
+              : null),
+        });
 
       const tracePart =
-        (stackLine
+        (traceLine
           ? '- ' +
-            stackLine
+            traceLine
               .trim()
               .replace(/file:\/\/.*\.js/, (match) => pathToFileURL(match))
           : '(no stack trace)');