import { t } from '@/i18n' import { useToastStore } from '@/stores/toastStore' export function useErrorHandling() { const toast = useToastStore() const toastErrorHandler = (error: any) => { console.error(error) toast.add({ severity: 'error', summary: t('g.error'), detail: error.message }) } const wrapWithErrorHandling = ( action: (...args: TArgs) => TReturn, errorHandler?: (error: any) => void, finallyHandler?: () => void ) => (...args: TArgs): TReturn | undefined => { try { return action(...args) } catch (e) { ;(errorHandler ?? toastErrorHandler)(e) } finally { finallyHandler?.() } } const wrapWithErrorHandlingAsync = ( action: (...args: TArgs) => Promise | TReturn, errorHandler?: (error: any) => void, finallyHandler?: () => void ) => async (...args: TArgs): Promise => { try { return await action(...args) } catch (e) { ;(errorHandler ?? toastErrorHandler)(e) } finally { finallyHandler?.() } } return { wrapWithErrorHandling, wrapWithErrorHandlingAsync } }