Line data Source code
1 : import { computed, reactive, toValue, watch } from 'vue'
2 :
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import { until } from '@vueuse/core'
6 :
7 : import { api } from '@/scripts/api'
8 : import { app } from '@/scripts/app'
9 : import { useSystemStatsStore } from '@/stores/systemStatsStore'
10 : import { createCancelToken } from '@/utils/createCancelToken'
11 : import { generateErrorReport } from '@/utils/errorReportUtil'
12 :
13 : import type { ErrorCardData } from './types'
14 :
15 : /** Fallback exception type for error reports when the backend does not provide one. Not i18n'd: used in diagnostic reports only. */
16 2355 : const FALLBACK_EXCEPTION_TYPE = 'Runtime Error'
17 :
18 6 : export function useErrorReport(cardSource: MaybeRefOrGetter<ErrorCardData>) {
19 6 : const systemStatsStore = useSystemStatsStore()
20 6 : const enrichedDetails = reactive<Partial<Record<number, string>>>({})
21 :
22 6 : const displayedDetailsMap = computed(() => {
23 12 : const card = toValue(cardSource)
24 12 : return Object.fromEntries(
25 : card.errors.map((error, idx) => [
26 : idx,
27 : enrichedDetails[idx] ?? error.displayDetails ?? error.details
28 : ])
29 : )
30 : })
31 :
32 6 : watch(
33 : () => toValue(cardSource),
34 : async (card, _, onCleanup) => {
35 6 : const { cancel, isCancelled } = createCancelToken()
36 6 : onCleanup(cancel)
37 :
38 6 : for (const key of Object.keys(enrichedDetails)) {
39 0 : delete enrichedDetails[key as unknown as number]
40 : }
41 :
42 6 : const runtimeErrors = card.errors
43 : .map((error, idx) => ({ error, idx }))
44 : .filter(({ error }) => error.isRuntimeError)
45 :
46 6 : if (runtimeErrors.length === 0) return
47 :
48 6 : if (!systemStatsStore.systemStats) {
49 0 : if (systemStatsStore.isLoading) {
50 0 : await until(() => systemStatsStore.isLoading).toBe(false)
51 : } else {
52 0 : try {
53 0 : await systemStatsStore.refetchSystemStats()
54 : } catch (e) {
55 0 : console.warn('Failed to fetch system stats for error report:', e)
56 0 : return
57 : }
58 : }
59 : }
60 6 : if (!systemStatsStore.systemStats || isCancelled()) return
61 :
62 6 : const logs = await api
63 : .getLogs()
64 : .catch(() => 'Failed to retrieve server logs')
65 6 : if (isCancelled()) return
66 :
67 6 : const workflow = (() => {
68 6 : try {
69 6 : return app.rootGraph.serialize()
70 : } catch (e) {
71 0 : console.warn('Failed to serialize workflow for error report:', e)
72 0 : return null
73 : }
74 : })()
75 6 : if (!workflow) return
76 :
77 6 : for (const { error, idx } of runtimeErrors) {
78 6 : try {
79 6 : const report = generateErrorReport({
80 : exceptionType: error.exceptionType ?? FALLBACK_EXCEPTION_TYPE,
81 : exceptionMessage: error.message,
82 : traceback: error.details,
83 : nodeId: card.nodeId,
84 : nodeType: card.title,
85 : systemStats: systemStatsStore.systemStats,
86 : serverLogs: logs,
87 : workflow
88 : })
89 6 : enrichedDetails[idx] = report
90 : } catch (e) {
91 0 : console.warn('Failed to generate error report:', e)
92 : }
93 : }
94 : },
95 : { immediate: true }
96 : )
97 :
98 6 : return { displayedDetailsMap }
99 : }
|