From e3358d7c6993aa454019ebf58a53198b9e28087f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 28 Nov 2023 12:30:42 -0400 Subject: sugar: extract "getUsefulStackLine" logic & internals --- src/util/sugar.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src/util/sugar.js') 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:/, + //, +]; + +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('')); + getUsefulStackLine(stack); const tracePart = (stackLine -- cgit 1.3.0-6-gf8a5