Skip to content
Snippets Groups Projects
Unverified Commit adb20868 authored by shall0pass's avatar shall0pass Committed by GitHub
Browse files

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.
parent ec3efc71
No related branches found
No related tags found
No related merge requests found
......@@ -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 };
}
}
---
category: Enhancements
authors: [shall0pass]
---
Improve error reporting for goal templates
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment