From adb20868cc29ef661a51adef57a6f6c78689fc8a Mon Sep 17 00:00:00 2001 From: shall0pass <20625555+shall0pass@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:01:57 -0500 Subject: [PATCH] Improve error reporting for goal templates (#895) This improves the error reporting when issues are found with Goal Templates. Before these changes, the only error that would be reported is the "Bills" error in the image while the other issues would be ignored and not funded. --- .../src/server/budget/goaltemplates.js | 50 ++++++++----------- upcoming-release-notes/895.md | 6 +++ 2 files changed, 26 insertions(+), 30 deletions(-) create mode 100644 upcoming-release-notes/895.md diff --git a/packages/loot-core/src/server/budget/goaltemplates.js b/packages/loot-core/src/server/budget/goaltemplates.js index 90ce80aa1..8d490e877 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.js +++ b/packages/loot-core/src/server/budget/goaltemplates.js @@ -54,16 +54,17 @@ async function processTemplate(month, force) { ].join('\n'), ), ); - let to_budget = await applyCategoryTemplate( - category, - template, - month, - force, - ); + let { amount: to_budget, errors: applyErrors } = + await applyCategoryTemplate(category, template, month, force); if (to_budget != null) { num_applied++; await setBudget({ category: category.id, month, amount: to_budget }); } + if (applyErrors != null) { + errors = errors.concat( + applyErrors.map(error => `${category.name}: ${error}`), + ); + } } } } @@ -129,6 +130,7 @@ async function getCategoryTemplates() { async function applyCategoryTemplate(category, template_lines, month, force) { let current_month = new Date(`${month}-01`); + let errors = []; // remove lines for past dates, calculate repeating dates template_lines = template_lines.filter(template => { @@ -156,12 +158,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) { num_months = differenceInCalendarMonths(target_month, current_month); } if (num_months < 0) { - console.log( - `${category.name}: ${`${template.month} is in the past:`} ${ - template.line - }`, - ); - return null; + errors.push(`${template.month} is in the past.`); + return false; } template.month = format(target_month, 'yyyy-MM'); if (spend_from) { @@ -170,7 +168,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) { break; default: } - return template; + return true; }); if (template_lines.length > 1) { @@ -200,10 +198,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) { // simple has 'monthly' and/or 'limit' params if (template.limit != null) { if (limit != null) { - console.log( - `${category.name}: More than one “up to†limit found. ${template.line}`, - ); - return null; + errors.push(`More than one “up to†limit found.`); + return { errors }; } else { limit = amountToInteger(template.limit); } @@ -244,10 +240,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) { let weeks = template.weeks != null ? Math.round(template.weeks) : 1; if (template.limit != null) { if (limit != null) { - console.log( - `${category.name}: More than one “up to†limit found. ${template.line}`, - ); - return null; + errors.push(`More than one “up to†limit found.`); + return { errors }; } else { limit = amountToInteger(template.limit); } @@ -299,12 +293,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) { let num_months = differenceInCalendarMonths(to_month, current_month); let target = amountToInteger(template.amount); if (num_months < 0) { - console.log( - `${category.name}: ${`${template.to} is in the past:`} ${ - template.line - }`, - ); - return null; + errors.push(`${template.month} is in the past.`); + return { errors }; } else if (num_months === 0) { to_budget = target - already_budgeted; } else { @@ -331,7 +321,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) { break; } case 'error': - return null; + return { errors }; default: } } @@ -347,7 +337,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) { to_budget === 0) && !force ) { - return null; + return { errors }; } else if (category.budgeted === to_budget && force) { return null; } else { @@ -359,6 +349,6 @@ async function applyCategoryTemplate(category, template_lines, month, force) { integerToAmount(last_month_balance + to_budget); str += ' ' + template_lines.map(x => x.line).join('\n'); console.log(str); - return to_budget; + return { amount: to_budget }; } } diff --git a/upcoming-release-notes/895.md b/upcoming-release-notes/895.md new file mode 100644 index 000000000..c807b4c03 --- /dev/null +++ b/upcoming-release-notes/895.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [shall0pass] +--- + +Improve error reporting for goal templates -- GitLab