Skip to content
Snippets Groups Projects
Unverified Commit e1f7262f authored by Jed Fox's avatar Jed Fox Committed by GitHub
Browse files

Add clear error to API when no budget is open (#1073)

Previously, this would fail with a confusing “cannot read property of
`undefined`” error.
parent 400078dc
No related branches found
No related tags found
No related merge requests found
...@@ -101,6 +101,12 @@ async function validateExpenseCategory(debug, id) { ...@@ -101,6 +101,12 @@ async function validateExpenseCategory(debug, id) {
} }
} }
function checkFileOpen() {
if (!(prefs.getPrefs() || {}).id) {
throw APIError('No budget file is open');
}
}
let batchPromise = null; let batchPromise = null;
handlers['api/batch-budget-start'] = async function () { handlers['api/batch-budget-start'] = async function () {
...@@ -225,6 +231,8 @@ handlers['api/start-import'] = async function ({ budgetName }) { ...@@ -225,6 +231,8 @@ handlers['api/start-import'] = async function ({ budgetName }) {
}; };
handlers['api/finish-import'] = async function () { handlers['api/finish-import'] = async function () {
checkFileOpen();
sheet.get().markCacheDirty(); sheet.get().markCacheDirty();
// We always need to fully reload the app. Importing doesn't touch // We always need to fully reload the app. Importing doesn't touch
...@@ -245,6 +253,8 @@ handlers['api/finish-import'] = async function () { ...@@ -245,6 +253,8 @@ handlers['api/finish-import'] = async function () {
handlers['api/abort-import'] = async function () { handlers['api/abort-import'] = async function () {
if (IMPORT_MODE) { if (IMPORT_MODE) {
checkFileOpen();
let { id } = prefs.getPrefs(); let { id } = prefs.getPrefs();
await handlers['close-budget'](); await handlers['close-budget']();
...@@ -256,15 +266,18 @@ handlers['api/abort-import'] = async function () { ...@@ -256,15 +266,18 @@ handlers['api/abort-import'] = async function () {
}; };
handlers['api/query'] = async function ({ query }) { handlers['api/query'] = async function ({ query }) {
checkFileOpen();
return aqlQuery(query); return aqlQuery(query);
}; };
handlers['api/budget-months'] = async function () { handlers['api/budget-months'] = async function () {
checkFileOpen();
let { start, end } = await handlers['get-budget-bounds'](); let { start, end } = await handlers['get-budget-bounds']();
return monthUtils.range(start, end); return monthUtils.range(start, end);
}; };
handlers['api/budget-month'] = async function ({ month }) { handlers['api/budget-month'] = async function ({ month }) {
checkFileOpen();
await validateMonth(month); await validateMonth(month);
let groups = await db.getCategoriesGrouped(); let groups = await db.getCategoriesGrouped();
...@@ -326,6 +339,7 @@ handlers['api/budget-set-amount'] = withMutation(async function ({ ...@@ -326,6 +339,7 @@ handlers['api/budget-set-amount'] = withMutation(async function ({
categoryId, categoryId,
amount, amount,
}) { }) {
checkFileOpen();
return handlers['budget/budget-amount']({ return handlers['budget/budget-amount']({
month, month,
category: categoryId, category: categoryId,
...@@ -338,6 +352,7 @@ handlers['api/budget-set-carryover'] = withMutation(async function ({ ...@@ -338,6 +352,7 @@ handlers['api/budget-set-carryover'] = withMutation(async function ({
categoryId, categoryId,
flag, flag,
}) { }) {
checkFileOpen();
await validateMonth(month); await validateMonth(month);
await validateExpenseCategory('budget-set-carryover', categoryId); await validateExpenseCategory('budget-set-carryover', categoryId);
return handlers['budget/set-carryover']({ return handlers['budget/set-carryover']({
...@@ -352,6 +367,7 @@ handlers['api/transactions-export'] = async function ({ ...@@ -352,6 +367,7 @@ handlers['api/transactions-export'] = async function ({
categoryGroups, categoryGroups,
payees, payees,
}) { }) {
checkFileOpen();
return handlers['transactions-export']({ return handlers['transactions-export']({
transactions, transactions,
categoryGroups, categoryGroups,
...@@ -363,6 +379,7 @@ handlers['api/transactions-import'] = withMutation(async function ({ ...@@ -363,6 +379,7 @@ handlers['api/transactions-import'] = withMutation(async function ({
accountId, accountId,
transactions, transactions,
}) { }) {
checkFileOpen();
return handlers['transactions-import']({ accountId, transactions }); return handlers['transactions-import']({ accountId, transactions });
}); });
...@@ -370,6 +387,7 @@ handlers['api/transactions-add'] = withMutation(async function ({ ...@@ -370,6 +387,7 @@ handlers['api/transactions-add'] = withMutation(async function ({
accountId, accountId,
transactions, transactions,
}) { }) {
checkFileOpen();
await addTransactions(accountId, transactions, { runTransfers: false }); await addTransactions(accountId, transactions, { runTransfers: false });
return 'ok'; return 'ok';
}); });
...@@ -379,6 +397,7 @@ handlers['api/transactions-get'] = async function ({ ...@@ -379,6 +397,7 @@ handlers['api/transactions-get'] = async function ({
startDate, startDate,
endDate, endDate,
}) { }) {
checkFileOpen();
let { data } = await aqlQuery( let { data } = await aqlQuery(
q('transactions') q('transactions')
.filter({ .filter({
...@@ -402,6 +421,7 @@ handlers['api/transaction-update'] = withMutation(async function ({ ...@@ -402,6 +421,7 @@ handlers['api/transaction-update'] = withMutation(async function ({
id, id,
fields, fields,
}) { }) {
checkFileOpen();
let { data } = await aqlQuery( let { data } = await aqlQuery(
q('transactions').filter({ id }).select('*').options({ splits: 'grouped' }), q('transactions').filter({ id }).select('*').options({ splits: 'grouped' }),
); );
...@@ -416,6 +436,7 @@ handlers['api/transaction-update'] = withMutation(async function ({ ...@@ -416,6 +436,7 @@ handlers['api/transaction-update'] = withMutation(async function ({
}); });
handlers['api/transaction-delete'] = withMutation(async function ({ id }) { handlers['api/transaction-delete'] = withMutation(async function ({ id }) {
checkFileOpen();
let { data } = await aqlQuery( let { data } = await aqlQuery(
q('transactions').filter({ id }).select('*').options({ splits: 'grouped' }), q('transactions').filter({ id }).select('*').options({ splits: 'grouped' }),
); );
...@@ -430,6 +451,7 @@ handlers['api/transaction-delete'] = withMutation(async function ({ id }) { ...@@ -430,6 +451,7 @@ handlers['api/transaction-delete'] = withMutation(async function ({ id }) {
}); });
handlers['api/accounts-get'] = async function () { handlers['api/accounts-get'] = async function () {
checkFileOpen();
let accounts = await db.getAccounts(); let accounts = await db.getAccounts();
return accounts.map(account => accountModel.toExternal(account)); return accounts.map(account => accountModel.toExternal(account));
}; };
...@@ -438,6 +460,7 @@ handlers['api/account-create'] = withMutation(async function ({ ...@@ -438,6 +460,7 @@ handlers['api/account-create'] = withMutation(async function ({
account, account,
initialBalance = null, initialBalance = null,
}) { }) {
checkFileOpen();
return handlers['account-create']({ return handlers['account-create']({
name: account.name, name: account.name,
type: account.type, type: account.type,
...@@ -450,6 +473,7 @@ handlers['api/account-create'] = withMutation(async function ({ ...@@ -450,6 +473,7 @@ handlers['api/account-create'] = withMutation(async function ({
}); });
handlers['api/account-update'] = withMutation(async function ({ id, fields }) { handlers['api/account-update'] = withMutation(async function ({ id, fields }) {
checkFileOpen();
return db.updateAccount({ id, ...accountModel.fromExternal(fields) }); return db.updateAccount({ id, ...accountModel.fromExternal(fields) });
}); });
...@@ -458,6 +482,7 @@ handlers['api/account-close'] = withMutation(async function ({ ...@@ -458,6 +482,7 @@ handlers['api/account-close'] = withMutation(async function ({
transferAccountId, transferAccountId,
transferCategoryId, transferCategoryId,
}) { }) {
checkFileOpen();
return handlers['account-close']({ return handlers['account-close']({
id, id,
transferAccountId, transferAccountId,
...@@ -466,16 +491,19 @@ handlers['api/account-close'] = withMutation(async function ({ ...@@ -466,16 +491,19 @@ handlers['api/account-close'] = withMutation(async function ({
}); });
handlers['api/account-reopen'] = withMutation(async function ({ id }) { handlers['api/account-reopen'] = withMutation(async function ({ id }) {
checkFileOpen();
return handlers['account-reopen']({ id }); return handlers['account-reopen']({ id });
}); });
handlers['api/account-delete'] = withMutation(async function ({ id }) { handlers['api/account-delete'] = withMutation(async function ({ id }) {
checkFileOpen();
return handlers['account-close']({ id, forced: true }); return handlers['account-close']({ id, forced: true });
}); });
handlers['api/categories-get'] = async function ({ handlers['api/categories-get'] = async function ({
grouped, grouped,
}: { grouped? } = {}) { }: { grouped? } = {}) {
checkFileOpen();
let result = await handlers['get-categories'](); let result = await handlers['get-categories']();
return grouped return grouped
? result.grouped.map(categoryGroupModel.toExternal) ? result.grouped.map(categoryGroupModel.toExternal)
...@@ -485,6 +513,7 @@ handlers['api/categories-get'] = async function ({ ...@@ -485,6 +513,7 @@ handlers['api/categories-get'] = async function ({
handlers['api/category-group-create'] = withMutation(async function ({ handlers['api/category-group-create'] = withMutation(async function ({
group, group,
}) { }) {
checkFileOpen();
return handlers['category-group-create']({ name: group.name }); return handlers['category-group-create']({ name: group.name });
}); });
...@@ -492,6 +521,7 @@ handlers['api/category-group-update'] = withMutation(async function ({ ...@@ -492,6 +521,7 @@ handlers['api/category-group-update'] = withMutation(async function ({
id, id,
fields, fields,
}) { }) {
checkFileOpen();
return handlers['category-group-update']({ return handlers['category-group-update']({
id, id,
...categoryGroupModel.fromExternal(fields), ...categoryGroupModel.fromExternal(fields),
...@@ -502,6 +532,7 @@ handlers['api/category-group-delete'] = withMutation(async function ({ ...@@ -502,6 +532,7 @@ handlers['api/category-group-delete'] = withMutation(async function ({
id, id,
transferCategoryId, transferCategoryId,
}) { }) {
checkFileOpen();
return handlers['category-group-delete']({ return handlers['category-group-delete']({
id, id,
transferId: transferCategoryId, transferId: transferCategoryId,
...@@ -509,6 +540,7 @@ handlers['api/category-group-delete'] = withMutation(async function ({ ...@@ -509,6 +540,7 @@ handlers['api/category-group-delete'] = withMutation(async function ({
}); });
handlers['api/category-create'] = withMutation(async function ({ category }) { handlers['api/category-create'] = withMutation(async function ({ category }) {
checkFileOpen();
return handlers['category-create']({ return handlers['category-create']({
name: category.name, name: category.name,
groupId: category.group_id, groupId: category.group_id,
...@@ -517,6 +549,7 @@ handlers['api/category-create'] = withMutation(async function ({ category }) { ...@@ -517,6 +549,7 @@ handlers['api/category-create'] = withMutation(async function ({ category }) {
}); });
handlers['api/category-update'] = withMutation(async function ({ id, fields }) { handlers['api/category-update'] = withMutation(async function ({ id, fields }) {
checkFileOpen();
return handlers['category-update']({ return handlers['category-update']({
id, id,
...categoryModel.fromExternal(fields), ...categoryModel.fromExternal(fields),
...@@ -527,6 +560,7 @@ handlers['api/category-delete'] = withMutation(async function ({ ...@@ -527,6 +560,7 @@ handlers['api/category-delete'] = withMutation(async function ({
id, id,
transferCategoryId, transferCategoryId,
}) { }) {
checkFileOpen();
return handlers['category-delete']({ return handlers['category-delete']({
id, id,
transferId: transferCategoryId, transferId: transferCategoryId,
...@@ -534,21 +568,25 @@ handlers['api/category-delete'] = withMutation(async function ({ ...@@ -534,21 +568,25 @@ handlers['api/category-delete'] = withMutation(async function ({
}); });
handlers['api/payees-get'] = async function () { handlers['api/payees-get'] = async function () {
checkFileOpen();
let payees = await handlers['payees-get'](); let payees = await handlers['payees-get']();
return payees.map(payeeModel.toExternal); return payees.map(payeeModel.toExternal);
}; };
handlers['api/payee-create'] = withMutation(async function ({ payee }) { handlers['api/payee-create'] = withMutation(async function ({ payee }) {
checkFileOpen();
return handlers['payee-create']({ name: payee.name }); return handlers['payee-create']({ name: payee.name });
}); });
handlers['api/payee-update'] = withMutation(async function ({ id, fields }) { handlers['api/payee-update'] = withMutation(async function ({ id, fields }) {
checkFileOpen();
return handlers['payees-batch-change']({ return handlers['payees-batch-change']({
updated: [{ id, ...payeeModel.fromExternal(fields) }], updated: [{ id, ...payeeModel.fromExternal(fields) }],
}); });
}); });
handlers['api/payee-delete'] = withMutation(async function ({ id }) { handlers['api/payee-delete'] = withMutation(async function ({ id }) {
checkFileOpen();
return handlers['payees-batch-change']({ deleted: [{ id }] }); return handlers['payees-batch-change']({ deleted: [{ id }] });
}); });
......
---
category: Maintenance
authors: [j-f1]
---
Add a clear error to the API when no budget is open, but you attempted to perform an action that requires a budget to be open.
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