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 ---
|
// --- 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() {
|
async function loadReports() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const { startDate, endDate } = dateRange.value
|
const { startDate, endDate } = dateRange.value
|
||||||
|
|
||||||
try {
|
const [s, ie, nw, cf] = await Promise.all([
|
||||||
const results = await Promise.allSettled([
|
fetchReport(reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value)),
|
||||||
reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value),
|
fetchReport(reportsApi.incomeVsExpense(startDate, endDate)),
|
||||||
reportsApi.incomeVsExpense(startDate, endDate),
|
fetchReport(reportsApi.netWorth(startDate, endDate)),
|
||||||
reportsApi.netWorth(startDate, endDate),
|
fetchReport(reportsApi.cashFlow(startDate, endDate)),
|
||||||
reportsApi.cashFlow(startDate, endDate),
|
])
|
||||||
])
|
|
||||||
|
|
||||||
spending.value = results[0].status === 'fulfilled' ? results[0].value.data : null
|
spending.value = s
|
||||||
incomeExpense.value = results[1].status === 'fulfilled' ? results[1].value.data : null
|
incomeExpense.value = ie
|
||||||
netWorth.value = results[2].status === 'fulfilled' ? results[2].value.data : null
|
netWorth.value = nw
|
||||||
cashFlow.value = results[3].status === 'fulfilled' ? results[3].value.data : null
|
cashFlow.value = cf
|
||||||
|
loading.value = false
|
||||||
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 {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadSpending() {
|
async function loadSpending() {
|
||||||
const { startDate, endDate } = dateRange.value
|
const { startDate, endDate } = dateRange.value
|
||||||
try {
|
spending.value = await fetchReport(reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value))
|
||||||
const { data } = await reportsApi.spendingByCategory(startDate, endDate, selectedAccountId.value)
|
|
||||||
spending.value = data
|
|
||||||
} catch {
|
|
||||||
showError('Failed to load spending report')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function showError(text: string) {
|
function showError(text: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user