« get me outta code hell

sugar: extract "getUsefulStackLine" logic & internals - 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-28 12:30:42 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-28 12:30:42 -0400
commite3358d7c6993aa454019ebf58a53198b9e28087f (patch)
tree08e115b2f0081e3f83cba138ab649ba7170c4a0f
parent6844d1275f0b0025b09ca909a99d705447792e1f (diff)
sugar: extract "getUsefulStackLine" logic & internals
-rw-r--r--src/util/sugar.js35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index eab44b75..cee3df12 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -589,6 +589,32 @@ export function _withAggregate(mode, aggregateOpts, fn) {
   }
 }
 
+export const unhelpfulStackLines = [
+  /sugar/,
+  /node:/,
+  /<anonymous>/,
+];
+
+export function getUsefulStackLine(stack) {
+  if (!stack) return '';
+
+  function isUseful(stackLine) {
+    const trimmed = stackLine.trim();
+
+    if (!trimmed.startsWith('at'))
+      return false;
+
+    if (unhelpfulStackLines.some(regex => regex.test(trimmed)))
+      return false;
+
+    return true;
+  }
+
+  const stackLines = stack.split('\n');
+  const usefulStackLine = stackLines.find(isUseful);
+  return usefulStackLine ?? '';
+}
+
 export function showAggregate(topError, {
   pathToFileURL = f => f,
   showTraces = true,
@@ -670,15 +696,8 @@ export function showAggregate(topError, {
         : messagePart);
 
     if (showTraces) {
-      const stackLines =
-        stack?.split('\n');
-
       const stackLine =
-        stackLines?.find(line =>
-          line.trim().startsWith('at') &&
-          !line.includes('sugar') &&
-          !line.includes('node:') &&
-          !line.includes('<anonymous>'));
+        getUsefulStackLine(stack);
 
       const tracePart =
         (stackLine