From b5386dea10c122ac1a71d82a77014cf37f8c11db Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 5 Jan 2024 22:00:34 -0400 Subject: sugar: refactor determineCause, determineHelpers --- src/util/sugar.js | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'src/util') diff --git a/src/util/sugar.js b/src/util/sugar.js index a447e6e7..26982eb6 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); -- cgit 1.3.0-6-gf8a5