From 32f5dfa4b3c12dec18d0655160f9d49ca93b16d9 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 6 Nov 2023 15:34:52 -0400 Subject: sugar: separate annotateError functions & utilities --- src/util/sugar.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/util/sugar.js b/src/util/sugar.js index 6718b697..d5f0fbd3 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -637,13 +637,31 @@ export function showAggregate(topError, { } } +export function annotateError(error, ...callbacks) { + for (const callback of callbacks) { + error = callback(error) ?? error; + } + + return error; +} + +export function annotateErrorWithIndex(error, index) { + return Object.assign(error, { + [Symbol.for('hsmusic.annotateError.indexInSourceArray')]: + index, + + message: + `(${colors.yellow(`#${index + 1}`)}) ` + + error.message, + }); +} + export function decorateErrorWithIndex(fn) { return (x, index, array) => { try { return fn(x, index, array); } catch (error) { - error.message = `(${colors.yellow(`#${index + 1}`)}) ${error.message}`; - error[Symbol.for('hsmusic.decorate.indexInSourceArray')] = index; + annotateErrorWithIndex(error, index); throw error; } }; @@ -660,6 +678,18 @@ export function decorateErrorWithCause(fn, cause) { }; } +export function annotateErrorWithFile(error, file) { + return Object.assign(error, { + [Symbol.for('hsmusic.annotateError.file')]: + file, + + message: + error.message + + (error.message.includes('\n') ? '\n' : ' ') + + `(file: ${colors.bright(colors.blue(file))})`, + }); +} + export function conditionallySuppressError(conditionFn, callbackFn) { return (...args) => { try { -- cgit 1.3.0-6-gf8a5