Files
ComfyUI_frontend/src/hooks/errorHooks.ts
Chenlei Hu 810a63f808 Support async hooks in TreeExplorerNode (#888)
* Support async hooks in TreeExplorerNode

* rebase

* nit

* Fix component test failure

* Add edit vitest

* Add more tests

* Add component test
2024-09-19 20:10:43 +09:00

42 lines
989 B
TypeScript

import { useToastStore } from '@/stores/toastStore'
import { useI18n } from 'vue-i18n'
export function useErrorHandling() {
const toast = useToastStore()
const { t } = useI18n()
const toastErrorHandler = (error: any) => {
toast.add({
severity: 'error',
summary: t('error'),
detail: error.message,
life: 3000
})
}
const wrapWithErrorHandling =
(action: (...args: any[]) => any, errorHandler?: (error: any) => void) =>
(...args: any[]) => {
try {
return action(...args)
} catch (e) {
;(errorHandler ?? toastErrorHandler)(e)
}
}
const wrapWithErrorHandlingAsync =
(
action: (...args: any[]) => Promise<any>,
errorHandler?: (error: any) => void
) =>
async (...args: any[]) => {
try {
return await action(...args)
} catch (e) {
;(errorHandler ?? toastErrorHandler)(e)
}
}
return { wrapWithErrorHandling, wrapWithErrorHandlingAsync }
}