standardize error handling across all stores

This commit is contained in:
2026-02-20 14:46:56 +02:00
parent db603577eb
commit 5cee94c893
8 changed files with 463 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { invoke } from '@tauri-apps/api/core'
import { handleInvokeError } from '../utils/errorHandler'
export interface Invoice {
id?: number
@@ -37,7 +38,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
try {
invoices.value = await invoke<Invoice[]>('get_invoices')
} catch (error) {
console.error('Failed to fetch invoices:', error)
handleInvokeError(error, 'Failed to fetch invoices', () => fetchInvoices())
} finally {
loading.value = false
}
@@ -49,7 +50,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
invoices.value.unshift({ ...invoice, id: Number(id) })
return Number(id)
} catch (error) {
console.error('Failed to create invoice:', error)
handleInvokeError(error, 'Failed to create invoice')
return null
}
}
@@ -63,7 +64,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
}
return true
} catch (error) {
console.error('Failed to update invoice:', error)
handleInvokeError(error, 'Failed to update invoice')
return false
}
}
@@ -77,7 +78,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
}
return true
} catch (error) {
console.error('Failed to update invoice template:', error)
handleInvokeError(error, 'Failed to update invoice template')
return false
}
}
@@ -88,7 +89,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
invoices.value = invoices.value.filter(i => i.id !== id)
return true
} catch (error) {
console.error('Failed to delete invoice:', error)
handleInvokeError(error, 'Failed to delete invoice')
return false
}
}
@@ -97,7 +98,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
try {
return await invoke<InvoiceItem[]>('get_invoice_items', { invoiceId })
} catch (error) {
console.error('Failed to fetch invoice items:', error)
handleInvokeError(error, 'Failed to fetch invoice items')
return []
}
}
@@ -106,7 +107,7 @@ export const useInvoicesStore = defineStore('invoices', () => {
try {
return await invoke<number>('create_invoice_item', { item })
} catch (error) {
console.error('Failed to create invoice item:', error)
handleInvokeError(error, 'Failed to create invoice item')
return null
}
}
@@ -123,6 +124,29 @@ export const useInvoicesStore = defineStore('invoices', () => {
}
}
async function updateStatus(id: number, status: string): Promise<boolean> {
try {
await invoke('update_invoice_status', { id, status })
const idx = invoices.value.findIndex(i => i.id === id)
if (idx !== -1) invoices.value[idx].status = status
return true
} catch (error) {
handleInvokeError(error, 'Failed to update invoice status')
return false
}
}
async function checkOverdue(): Promise<number> {
try {
const today = new Date().toISOString().split('T')[0]
const count = await invoke<number>('check_overdue_invoices', { today })
if (count > 0) await fetchInvoices()
return count
} catch {
return 0
}
}
return {
invoices,
loading,
@@ -133,6 +157,8 @@ export const useInvoicesStore = defineStore('invoices', () => {
deleteInvoice,
getInvoiceItems,
createInvoiceItem,
saveInvoiceItems
saveInvoiceItems,
updateStatus,
checkOverdue
}
})