Line data Source code
1 : import type { ErrorEvent } from '@sentry/vue'
2 :
3 2356 : const VITE_PRELOAD_ERROR = 'vite_preload_error'
4 2356 : const PRELOAD_BREADCRUMB_PREFIX = `[Reported error]: ${VITE_PRELOAD_ERROR}`
5 2356 : const FOLLOW_ON_WINDOW_SECONDS = 5
6 2356 : const UNDEFINED_FOLLOW_ON_PATTERNS = [
7 : /^Cannot destructure property .+ as it is undefined\.?$/,
8 : /^Cannot read propert(?:y|ies) of undefined\b/,
9 : /^undefined is not an object \(evaluating .+\)$/,
10 : /^undefined has no properties$/
11 : ]
12 :
13 0 : function isUndefinedFollowOn(event: ErrorEvent): boolean {
14 : const exceptions = event.exception?.values ?? []
15 0 : const value = exceptions.at(-1)?.value
16 0 : return (
17 : typeof value === 'string' &&
18 : UNDEFINED_FOLLOW_ON_PATTERNS.some((pattern) => pattern.test(value))
19 : )
20 : }
21 :
22 0 : export function tagSentryStaleChunkCause(event: ErrorEvent): ErrorEvent {
23 0 : try {
24 0 : if (
25 : event.tags?.error_type === VITE_PRELOAD_ERROR ||
26 : !event.timestamp ||
27 : !isUndefinedFollowOn(event)
28 : ) {
29 0 : return event
30 : }
31 :
32 0 : const breadcrumb = [...(event.breadcrumbs ?? [])]
33 : .reverse()
34 : .find(({ message }) => message?.startsWith(PRELOAD_BREADCRUMB_PREFIX))
35 0 : if (!breadcrumb?.timestamp) return event
36 :
37 0 : const age = event.timestamp - breadcrumb.timestamp
38 0 : if (age < 0 || age > FOLLOW_ON_WINDOW_SECONDS) return event
39 :
40 0 : return {
41 : ...event,
42 : tags: { ...event.tags, error_cause: VITE_PRELOAD_ERROR }
43 : }
44 : } catch {
45 0 : return event
46 : }
47 : }
|