mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-03-13 17:10:06 +00:00
Extract error handling with toast message as hook (#825)
This commit is contained in:
28
src/hooks/errorHooks.ts
Normal file
28
src/hooks/errorHooks.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useToast } from 'primevue/usetoast'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
export function useErrorHandling() {
|
||||
const toast = useToast()
|
||||
const { t } = useI18n()
|
||||
|
||||
const wrapWithErrorHandling =
|
||||
(action: (...args: any[]) => any, errorHandler?: (error: any) => void) =>
|
||||
(...args: any[]) => {
|
||||
try {
|
||||
return action(...args)
|
||||
} catch (e) {
|
||||
if (errorHandler) {
|
||||
errorHandler(e)
|
||||
} else {
|
||||
toast.add({
|
||||
severity: 'error',
|
||||
summary: t('error'),
|
||||
detail: e.message,
|
||||
life: 3000
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { wrapWithErrorHandling }
|
||||
}
|
||||
Reference in New Issue
Block a user