From 6f1642dd0f1fc79b05b98fb892d6258cfffc7e15 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 09:51:40 -0400 Subject: sugar: revamp showAggregate helpful trace line algorithm --- src/util/sugar.js | 89 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 20 deletions(-) (limited to 'src') 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:/, //, ]; -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)'); -- cgit 1.3.0-6-gf8a5