Fix reports view showing error snackbar when no data exists
Wrap each report API call in a fetchReport helper that catches errors individually and returns null, treating 4xx responses as empty data instead of surfacing an error message to the user. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -457,42 +457,42 @@ const cashFlowChartOptions = computed(() => ({
|
||||
}))
|
||||
|
||||
// --- Data loading ---
|
||||
async function fetchReport<T>(request: Promise<{ data: T }>): Promise<T | null> {
|
||||
try {
|
||||
const { data } = await request
|
||||
return data
|
||||
} catch (err: any) {
|
||||
const status = err?.response?.status
|
||||
if (status && status >= 500) {
|
||||
console.error('Report request failed:', err)
|
||||
return null
|
||||
}
|
||||
// 4xx (including 404) treated as no data
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function loadReports() {
|
||||
loading.value = true
|
||||
const { startDate, endDate } = dateRange.value
|
||||
|
||||
try {
|
||||
const results = await Promise.allSettled([
|
||||
reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value),
|
||||
reportsApi.incomeVsExpense(startDate, endDate),
|
||||
reportsApi.netWorth(startDate, endDate),
|
||||
reportsApi.cashFlow(startDate, endDate),
|
||||
const [s, ie, nw, cf] = await Promise.all([
|
||||
fetchReport(reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value)),
|
||||
fetchReport(reportsApi.incomeVsExpense(startDate, endDate)),
|
||||
fetchReport(reportsApi.netWorth(startDate, endDate)),
|
||||
fetchReport(reportsApi.cashFlow(startDate, endDate)),
|
||||
])
|
||||
|
||||
spending.value = results[0].status === 'fulfilled' ? results[0].value.data : null
|
||||
incomeExpense.value = results[1].status === 'fulfilled' ? results[1].value.data : null
|
||||
netWorth.value = results[2].status === 'fulfilled' ? results[2].value.data : null
|
||||
cashFlow.value = results[3].status === 'fulfilled' ? results[3].value.data : null
|
||||
|
||||
const failed = results.filter(r => r.status === 'rejected')
|
||||
if (failed.length > 0) {
|
||||
showError('Some reports failed to load')
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to load reports')
|
||||
} finally {
|
||||
spending.value = s
|
||||
incomeExpense.value = ie
|
||||
netWorth.value = nw
|
||||
cashFlow.value = cf
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSpending() {
|
||||
const { startDate, endDate } = dateRange.value
|
||||
try {
|
||||
const { data } = await reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value)
|
||||
spending.value = data
|
||||
} catch {
|
||||
showError('Failed to load spending report')
|
||||
}
|
||||
spending.value = await fetchReport(reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value))
|
||||
}
|
||||
|
||||
function showError(text: string) {
|
||||
|
||||
Reference in New Issue
Block a user