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:
parent
212dc77df7
commit
c1fc9309b1
1 changed files with 57 additions and 37 deletions
|
|
@ -79,7 +79,15 @@ export default function DataImport() {
|
||||||
try {
|
try {
|
||||||
setBackgroundProcessing('Processing expenses from uploaded files...');
|
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[] = [];
|
const expensesToCreate: any[] = [];
|
||||||
|
|
||||||
if (etsyFile) {
|
if (etsyFile) {
|
||||||
|
|
@ -88,23 +96,27 @@ export default function DataImport() {
|
||||||
|
|
||||||
// Add Etsy fees (check for duplicates)
|
// Add Etsy fees (check for duplicates)
|
||||||
etsyFees.forEach((fee: EtsyFeeRecord) => {
|
etsyFees.forEach((fee: EtsyFeeRecord) => {
|
||||||
const isDuplicate = existingExpenses.some((expense: any) =>
|
try {
|
||||||
expense.description === fee.description &&
|
const isDuplicate = existingExpenses.some((expense: any) =>
|
||||||
Math.abs(expense.amount - fee.amount) < 0.01 &&
|
expense.description === fee.description &&
|
||||||
new Date(expense.date).toDateString() === new Date(fee.date).toDateString() &&
|
Math.abs(expense.amount - fee.amount) < 0.01 &&
|
||||||
expense.vendor === fee.vendor
|
new Date(expense.date).toDateString() === new Date(fee.date).toDateString() &&
|
||||||
);
|
expense.vendor === fee.vendor
|
||||||
|
);
|
||||||
if (!isDuplicate) {
|
|
||||||
expensesToCreate.push({
|
if (!isDuplicate) {
|
||||||
description: fee.description,
|
expensesToCreate.push({
|
||||||
amount: fee.amount,
|
description: fee.description,
|
||||||
category: fee.category,
|
amount: fee.amount,
|
||||||
date: fee.date,
|
category: fee.category,
|
||||||
taxDeductible: fee.taxDeductible,
|
date: fee.date,
|
||||||
vendor: fee.vendor,
|
taxDeductible: fee.taxDeductible,
|
||||||
reference: fee.reference
|
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)
|
// Add shipping expenses (check for duplicates)
|
||||||
shippingRecords.forEach((shipping: ParsedShippingRecord) => {
|
shippingRecords.forEach((shipping: ParsedShippingRecord) => {
|
||||||
if (shipping.totalCost > 0) {
|
try {
|
||||||
const isDuplicate = existingExpenses.some((expense: any) =>
|
if (shipping.totalCost > 0) {
|
||||||
expense.reference === shipping.trackingNumber &&
|
const isDuplicate = existingExpenses.some((expense: any) =>
|
||||||
expense.vendor === 'Australia Post' &&
|
expense.reference === shipping.trackingNumber &&
|
||||||
Math.abs(expense.amount - shipping.totalCost) < 0.01 &&
|
expense.vendor === 'Australia Post' &&
|
||||||
new Date(expense.date).toDateString() === new Date(shipping.date).toDateString()
|
Math.abs(expense.amount - shipping.totalCost) < 0.01 &&
|
||||||
);
|
new Date(expense.date).toDateString() === new Date(shipping.date).toDateString()
|
||||||
|
);
|
||||||
if (!isDuplicate) {
|
|
||||||
expensesToCreate.push({
|
if (!isDuplicate) {
|
||||||
description: `Australia Post - ${shipping.trackingNumber}`,
|
expensesToCreate.push({
|
||||||
amount: shipping.totalCost,
|
description: `Australia Post - ${shipping.trackingNumber}`,
|
||||||
category: 'Shipping & Postage',
|
amount: shipping.totalCost,
|
||||||
date: shipping.date,
|
category: 'Shipping & Postage',
|
||||||
taxDeductible: true,
|
date: shipping.date,
|
||||||
vendor: 'Australia Post',
|
taxDeductible: true,
|
||||||
reference: shipping.trackingNumber
|
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) {
|
} catch (error) {
|
||||||
console.error('Error in background expense processing:', 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 {
|
} finally {
|
||||||
setBackgroundProcessing('');
|
setBackgroundProcessing('');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue