Line data Source code
1 : import type { ErrorEvent, EventHint } from '@sentry/vue'
2 :
3 : import { isThirdPartyErrorNoise } from '@comfyorg/shared-frontend-utils/telemetry'
4 :
5 : import { isAbortError } from '@/utils/typeGuardUtil'
6 :
7 : import { classifyTelemetryErrorOrigin } from './errorOrigin'
8 :
9 0 : function messageFrom(value: unknown): string | undefined {
10 0 : try {
11 0 : if (typeof value === 'string') return value
12 0 : if (
13 : typeof value === 'object' &&
14 : value !== null &&
15 : 'message' in value &&
16 : typeof value.message === 'string'
17 : )
18 0 : return value.message
19 0 : return undefined
20 : } catch {
21 0 : return undefined
22 : }
23 : }
24 :
25 0 : function exceptionValueFrom(value: unknown): string[] {
26 0 : try {
27 0 : if (typeof value !== 'object' || value === null || !('value' in value))
28 0 : return []
29 0 : return typeof value.value === 'string' ? [value.value] : []
30 : } catch {
31 0 : return []
32 : }
33 : }
34 :
35 0 : function exceptionTypeFrom(value: unknown): unknown {
36 0 : try {
37 0 : return typeof value === 'object' && value !== null && 'type' in value
38 : ? value.type
39 : : undefined
40 : } catch {
41 0 : return undefined
42 : }
43 : }
44 :
45 : /**
46 : * Drops a browser-extension messaging failure that the app never emits, and
47 : * events whose thrown error is an AbortError. That also drops timeouts
48 : * implemented as a bare `controller.abort()`; the noise reduction is worth it.
49 : * Chained causes are ignored so a first-party error wrapping one is kept.
50 : */
51 0 : export function sentryThirdPartyErrorFilter(
52 : event: ErrorEvent,
53 : hint: EventHint
54 : ): ErrorEvent | null {
55 0 : try {
56 0 : if (
57 : isAbortError(hint.originalException) ||
58 : exceptionTypeFrom(event.exception?.values?.at(-1)) === 'AbortError'
59 : )
60 0 : return null
61 0 : if (
62 : isThirdPartyErrorNoise(messageFrom(hint.originalException)) ||
63 : isThirdPartyErrorNoise(event.message)
64 : )
65 0 : return null
66 0 : const exceptionMessages =
67 : event.exception?.values?.flatMap(exceptionValueFrom) ?? []
68 0 : return exceptionMessages.length > 0 &&
69 : exceptionMessages.every(isThirdPartyErrorNoise)
70 : ? null
71 : : event
72 : } catch {
73 0 : return event
74 : }
75 : }
76 :
77 0 : export function tagSentryNonFirstPartyOrigin(event: ErrorEvent): ErrorEvent {
78 0 : try {
79 0 : const frame = event.exception?.values?.at(-1)?.stacktrace?.frames?.at(-1)
80 0 : const path = frame?.abs_path ?? frame?.filename
81 0 : if (
82 : !path ||
83 : path.trim() === '' ||
84 : path === '<anonymous>' ||
85 : path === '[native code]'
86 : )
87 0 : return event
88 :
89 0 : const origin = classifyTelemetryErrorOrigin(path)
90 0 : if (origin.origin === 'first_party') return event
91 :
92 0 : return {
93 : ...event,
94 : tags: {
95 : ...event.tags,
96 : error_origin: origin.origin,
97 : ...(origin.origin === 'extension' && {
98 : extension: origin.extension
99 : })
100 : }
101 : }
102 : } catch {
103 0 : return event
104 : }
105 : }
|