fix: improve error handling in background expense processing

- Add proper array validation for existingExpenses API response
- Handle different API response structures (data.expenses vs data)
- Add try-catch blocks around individual expense processing loops
- Improve error messages to show specific error details
- Prevent TypeError when API response format is unexpected

Resolves 'gt.some is not a function' error in background processing.
This commit is contained in:
dlawler489 2026-05-05 20:55:33 +10:00
parent 212dc77df7
commit c1fc9309b1

View file

@ -79,7 +79,15 @@ export default function DataImport() {
try {
setBackgroundProcessing('Processing expenses from uploaded files...');
const existingExpenses = await api.get('/expenses').then(res => res.data);
const existingExpensesResponse = await api.get('/expenses');
const existingExpenses = existingExpensesResponse.data?.expenses || existingExpensesResponse.data || [];
// Ensure existingExpenses is an array
if (!Array.isArray(existingExpenses)) {
console.warn('Expected existingExpenses to be an array, got:', typeof existingExpenses, existingExpenses);
throw new Error('Invalid response format for existing expenses');
}
const expensesToCreate: any[] = [];
if (etsyFile) {
@ -88,23 +96,27 @@ export default function DataImport() {
// Add Etsy fees (check for duplicates)
etsyFees.forEach((fee: EtsyFeeRecord) => {
const isDuplicate = existingExpenses.some((expense: any) =>
expense.description === fee.description &&
Math.abs(expense.amount - fee.amount) < 0.01 &&
new Date(expense.date).toDateString() === new Date(fee.date).toDateString() &&
expense.vendor === fee.vendor
);
try {
const isDuplicate = existingExpenses.some((expense: any) =>
expense.description === fee.description &&
Math.abs(expense.amount - fee.amount) < 0.01 &&
new Date(expense.date).toDateString() === new Date(fee.date).toDateString() &&
expense.vendor === fee.vendor
);
if (!isDuplicate) {
expensesToCreate.push({
description: fee.description,
amount: fee.amount,
category: fee.category,
date: fee.date,
taxDeductible: fee.taxDeductible,
vendor: fee.vendor,
reference: fee.reference
});
if (!isDuplicate) {
expensesToCreate.push({
description: fee.description,
amount: fee.amount,
category: fee.category,
date: fee.date,
taxDeductible: fee.taxDeductible,
vendor: fee.vendor,
reference: fee.reference
});
}
} catch (error) {
console.error('Error processing Etsy fee:', fee, error);
}
});
}
@ -115,25 +127,29 @@ export default function DataImport() {
// Add shipping expenses (check for duplicates)
shippingRecords.forEach((shipping: ParsedShippingRecord) => {
if (shipping.totalCost > 0) {
const isDuplicate = existingExpenses.some((expense: any) =>
expense.reference === shipping.trackingNumber &&
expense.vendor === 'Australia Post' &&
Math.abs(expense.amount - shipping.totalCost) < 0.01 &&
new Date(expense.date).toDateString() === new Date(shipping.date).toDateString()
);
try {
if (shipping.totalCost > 0) {
const isDuplicate = existingExpenses.some((expense: any) =>
expense.reference === shipping.trackingNumber &&
expense.vendor === 'Australia Post' &&
Math.abs(expense.amount - shipping.totalCost) < 0.01 &&
new Date(expense.date).toDateString() === new Date(shipping.date).toDateString()
);
if (!isDuplicate) {
expensesToCreate.push({
description: `Australia Post - ${shipping.trackingNumber}`,
amount: shipping.totalCost,
category: 'Shipping & Postage',
date: shipping.date,
taxDeductible: true,
vendor: 'Australia Post',
reference: shipping.trackingNumber
});
if (!isDuplicate) {
expensesToCreate.push({
description: `Australia Post - ${shipping.trackingNumber}`,
amount: shipping.totalCost,
category: 'Shipping & Postage',
date: shipping.date,
taxDeductible: true,
vendor: 'Australia Post',
reference: shipping.trackingNumber
});
}
}
} catch (error) {
console.error('Error processing shipping record:', shipping, error);
}
});
}
@ -169,7 +185,11 @@ export default function DataImport() {
}
} catch (error) {
console.error('Error in background expense processing:', error);
toast.error('Error processing expenses in background');
if (error instanceof Error) {
toast.error(`Error processing expenses: ${error.message}`);
} else {
toast.error('Error processing expenses in background');
}
} finally {
setBackgroundProcessing('');
}