Skip to content

A local-first personal finance app

Fix error spread in non-batch bank sync (#4689)

Mario Lamacchia31ed12832cf0
diff --git a/packages/loot-core/src/server/api.test.ts b/packages/loot-core/src/server/api.test.ts
new file mode 100644
index 0000000000..674a413727
--- /dev/null
+++ b/packages/loot-core/src/server/api.test.ts
@@ -0,0 +1,36 @@
+import { getBankSyncError } from '../shared/errors';
+import { ServerHandlers } from '../types/server-handlers';
+
+import { installAPI } from './api';
+jest.mock('../shared/errors', () => ({
+ getBankSyncError: jest.fn(error => `Bank sync error: ${error}`),
+}));
+
+describe('API handlers', () => {
+ const handlers = installAPI({} as unknown as ServerHandlers);
+
+ describe('api/bank-sync', () => {
+ it('should sync a single account when accountId is provided', async () => {
+ handlers['accounts-bank-sync'] = jest
+ .fn()
+ .mockResolvedValue({ errors: [] });
+
+ await handlers['api/bank-sync']({ accountId: 'account1' });
+ expect(handlers['accounts-bank-sync']).toHaveBeenCalledWith({
+ ids: ['account1'],
+ });
+ });
+
+ it('should handle errors in non batch sync', async () => {
+ handlers['accounts-bank-sync'] = jest.fn().mockResolvedValue({
+ errors: ['connection-failed'],
+ });
+
+ await expect(
+ handlers['api/bank-sync']({ accountId: 'account2' }),
+ ).rejects.toThrow('Bank sync error: connection-failed');
+
+ expect(getBankSyncError).toHaveBeenCalledWith('connection-failed');
+ });
+ });
+});
diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.ts
index e03039f278..a016cf143f 100644
--- a/packages/loot-core/src/server/api.ts
+++ b/packages/loot-core/src/server/api.ts
@@ -258,7 +258,7 @@ handlers['api/bank-sync'] = async function (args) {
ids: [args.accountId],
});
- allErrors.push(errors);
+ allErrors.push(...errors);
} else {
const accountsData = await handlers['accounts-get']();
const accountIdsToSync = accountsData.map(a => a.id);
diff --git a/upcoming-release-notes/4689.md b/upcoming-release-notes/4689.md
new file mode 100644
index 0000000000..8515c57851
--- /dev/null
+++ b/upcoming-release-notes/4689.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [mariolamacchia]
+---
+
+Fixed an issue in the bank sync API where errors weren't being properly collected during bank sync, which led to non-batch syncs to always fail.