import { t } from '@/i18n' import { useToastStore } from '@/platform/updates/common/toastStore' export function useErrorHandling() { const toast = useToastStore() const toastErrorHandler = (error: unknown) => { toast.add({ severity: 'error', summary: t('g.error'), detail: error instanceof Error ? error.message : t('g.unknownError') }) console.error(error) } 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, toastErrorHandler } }