[AI] Fix SimpleFIN batch sync error_code TypeError

Fix "Cannot read properties of undefined (reading 'error_code')" that
occurs during SimpleFIN batch sync by:

1. Adding null check for downloadSimpleFinTransactions result in
   simpleFinBatchSync (sync.ts) - the function can return undefined
   when user token is missing

2. Adding .catch() handler on individual processBankSyncDownload
   promises so a single account failure doesn't crash the entire
   batch via Promise.all rejection

3. Using optional chaining on syncResponse.res?.error_code in app.ts
   and handling the case where res is undefined with proper error
   reporting

https://claude.ai/code/session_01XbHgxxrXYR3UTyW6VmYj47
This commit is contained in:
Claude
2026-03-09 20:50:29 +00:00
parent 3c32429dfb
commit 1f2f8c5f10
2 changed files with 31 additions and 6 deletions

View File

@@ -1069,7 +1069,7 @@ async function simpleFinBatchSync({
const matchedTransactions: Array<TransactionEntity['id']> = [];
const updatedAccounts: Array<AccountEntity['id']> = [];
if (syncResponse.res.error_code) {
if (syncResponse.res?.error_code) {
errors.push(
handleSyncError(
{
@@ -1081,7 +1081,7 @@ async function simpleFinBatchSync({
account,
),
);
} else {
} else if (syncResponse.res) {
const syncResponseData = await handleSyncResponse(
syncResponse.res,
account,
@@ -1090,6 +1090,15 @@ async function simpleFinBatchSync({
newTransactions.push(...syncResponseData.newTransactions);
matchedTransactions.push(...syncResponseData.matchedTransactions);
updatedAccounts.push(...syncResponseData.updatedAccounts);
} else {
errors.push(
handleSyncError(
new Error(
'Failed syncing account "' + account.name + '": empty response',
),
account,
),
);
}
retVal.push({

View File

@@ -1092,6 +1092,16 @@ export async function simpleFinBatchSync(
startDates,
);
if (!res) {
return accounts.map(account => ({
accountId: account.id,
res: {
error_type: 'NO_DATA',
error_code: 'NO_DATA',
},
}));
}
const promises = [];
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
@@ -1126,12 +1136,18 @@ export async function simpleFinBatchSync(
}
promises.push(
processBankSyncDownload(download, account.id, acctRow, newAccount).then(
res => ({
processBankSyncDownload(download, account.id, acctRow, newAccount)
.then(res => ({
accountId: account.id,
res,
}),
),
}))
.catch(err => ({
accountId: account.id,
res: {
error_type: err?.category || 'INTERNAL_ERROR',
error_code: err?.code || 'INTERNAL_ERROR',
},
})),
);
}