Line data Source code
1 : import type { ErrorEvent, EventHint } from '@sentry/vue'
2 :
3 2356 : const MAX_SCRIPT_PATHS = 250
4 :
5 : export interface VueDirectiveRuntimeContext {
6 : frontend_version: string
7 : frontend_commit: string
8 : loaded_script_count: number
9 : first_party_script_paths: string[]
10 : }
11 :
12 2356 : export const VUE_DIRECTIVE_DIAGNOSTIC_TAG = 'vue_directive_runtime'
13 :
14 0 : function eventMessages(event: ErrorEvent, hint: EventHint): string[] {
15 0 : const messages = [event.message]
16 0 : const originalException = hint.originalException
17 0 : if (originalException instanceof Error)
18 0 : messages.push(originalException.message)
19 0 : if (typeof originalException === 'string') messages.push(originalException)
20 0 : event.exception?.values?.forEach((exception) => {
21 0 : messages.push(exception.value)
22 : })
23 0 : return messages.filter((message) => message !== undefined)
24 : }
25 :
26 0 : function isVueDirectiveFailure(event: ErrorEvent, hint: EventHint): boolean {
27 0 : const frames =
28 : event.exception?.values?.flatMap(
29 : (exception) => exception.stacktrace?.frames ?? []
30 : ) ?? []
31 0 : if (frames.some((frame) => frame.function?.includes('withDirectives')))
32 0 : return true
33 :
34 : const hasVueCoreFrame = frames.some((frame) =>
35 : frame.filename?.includes('vendor-vue-core')
36 : )
37 0 : return (
38 : hasVueCoreFrame &&
39 : eventMessages(event, hint).some((message) =>
40 : message.includes('undefined is not a function')
41 : )
42 : )
43 : }
44 :
45 0 : function firstPartyScriptPaths(): string[] {
46 0 : try {
47 0 : return performance
48 : .getEntriesByType('resource')
49 : .map((entry) => new URL(entry.name, window.location.href))
50 : .filter(
51 : (url) =>
52 : url.origin === window.location.origin && url.pathname.endsWith('.js')
53 : )
54 : .map((url) => url.pathname)
55 : .slice(0, MAX_SCRIPT_PATHS)
56 : } catch {
57 0 : return []
58 : }
59 : }
60 :
61 0 : export function addVueDirectiveDiagnostics(
62 : event: ErrorEvent,
63 : hint: EventHint
64 : ): ErrorEvent {
65 0 : if (!isVueDirectiveFailure(event, hint)) return event
66 :
67 0 : const scriptPaths = firstPartyScriptPaths()
68 0 : return {
69 : ...event,
70 : tags: {
71 : ...event.tags,
72 : diagnostic: VUE_DIRECTIVE_DIAGNOSTIC_TAG
73 : },
74 : contexts: {
75 : ...event.contexts,
76 : [VUE_DIRECTIVE_DIAGNOSTIC_TAG]: {
77 : frontend_version: __COMFYUI_FRONTEND_VERSION__,
78 : frontend_commit: __COMFYUI_FRONTEND_COMMIT__,
79 : loaded_script_count: scriptPaths.length,
80 : first_party_script_paths: scriptPaths
81 : } satisfies VueDirectiveRuntimeContext
82 : }
83 : }
84 : }
|