/** * Adapted from * @see https://github.com/vuejs/core/blob/b50eb68c50f3b94dca2e96f706c3e96ab864df24/packages/runtime-core/src/component.ts * @see https://github.com/vuejs/core/blob/ac9e7e8bfa55432a73a10864805fdf48bda2ff73/packages/runtime-core/src/warning.ts * @see https://github.com/getsentry/sentry-javascript/blob/fbadeb81d23c03c3d852531c2deef59a3a992def/packages/vue/src/errorhandler.ts * * Original licenses: * * Copyright (c) 2018-present, Yuxi (Evan) You * @license MIT * * Copyright (c) 2019 Functional Software, Inc. dba Sentry * @license MIT */ import * as Sentry from '@sentry/browser' import { createSentryPiniaPlugin } from '@sentry/vue' import { pauseTracking, resetTracking } from '@vue/reactivity' import { getActivePinia } from 'pinia' import { type Component, type ComponentInternalInstance, type ComponentPublicInstance, type ConcreteComponent, type MaybeRefOrGetter, type VNode, toValue } from 'vue' import { useError } from '../stores/Error' import { getHttpStatusCode } from '../support/Http' type TraceEntry = { vnode: VNode; recurseCount: number } type ComponentTraceStack = TraceEntry[] const classifyRE = /(?:^|[-_])\w/g function classify(str: string): string { return str.replace(classifyRE, (c) => c.toUpperCase()).replace(/[-_]/g, '') } function getComponentName(Component: ConcreteComponent): string | undefined { return typeof Component === 'function' ? Component.displayName || Component.name : Component.name || Component.__name } function formatComponentName(instance: ComponentInternalInstance | null): string { if (!instance) { return 'Anonymous' } const Component = instance.type const isRoot = instance.parent == null let name = getComponentName(Component) if (!name && Component.__file) { const match = Component.__file.match(/([^/\\]+)\.\w+$/) if (match) { name = match[1] } } if (!name && instance) { const inferFromRegistry = (registry?: Record) => { for (const key in registry) { if (registry[key] === Component) { return key } } } name = // @ts-expect-error internal API inferFromRegistry(instance.components) // @ts-expect-error internal API || inferFromRegistry(instance.parent?.type.components) || inferFromRegistry(instance.appContext.components) } return name ? classify(name) : isRoot ? 'App' : 'Anonymous' } function getComponentTrace(currentVNode: VNode | null): ComponentTraceStack { if (!currentVNode) { return [] } const normalizedStack: ComponentTraceStack = [] while (currentVNode) { const last = normalizedStack[0] if (last && last.vnode === currentVNode) { last.recurseCount++ } else { normalizedStack.push({ vnode: currentVNode, recurseCount: 0 }) } const parentInstance: ComponentInternalInstance | null = currentVNode.component && currentVNode.component.parent currentVNode = parentInstance && parentInstance.vnode } return normalizedStack } function formatTrace(instance: ComponentInternalInstance | null): string { return getComponentTrace(instance && instance.vnode) .map(formatTraceEntry) .join('\n') } function formatTraceEntry({ vnode, recurseCount }: TraceEntry): string { return `at <${formatComponentName(vnode.component)}${vnode.props ? ` ${formatProps(vnode.props)}` : ''}>\ ${recurseCount > 0 ? ` ... (${recurseCount} recursive call${recurseCount > 1 ? 's' : ''})` : ''}` } function formatProps(props: Record): string { return Object.keys(props) .map((key) => { let value = props[key] if (typeof value === 'string') { value = JSON.stringify(value) return `${key}=${value}` } else if (typeof value === 'function') { return `:${key}="fn${value.name ? `<${value.name}>` : ''}"` } else if (typeof value !== 'object' || value === null) { return `:${key}="${String(value)}"` } else { return `${key}="..."` } }) .join(' ') } const ignoreErrors = [ /Network Error/, /Non-Error (?:exception|promise rejection) captured/, /ResizeObserver loop/, /Can't find variable: gmo/, /\[Cloudflare Turnstile\] Error: (?:10[2-46]|1106[02]|[36]00)/, /window\.webkit\.messageHandlers/ ] const stringifiers: readonly ((e: unknown) => string | false)[] = [ (e) => typeof e === 'string' && e, (e) => e instanceof Error && (e.message || e.name), (e) => { const json = JSON.stringify(e) return json !== '{}' && json }, (e) => String(e) ] function getErrorMessage(error: unknown | undefined): string { for (const stringify of stringifiers) { try { const s = stringify(error) if (s) { return s } } catch { /* continue */ } } return 'unknown error' } export function useErrorHandler({ dsn, environment, user }: { dsn?: string environment?: string user?: MaybeRefOrGetter }) { const error = useError() function set(value: any) { const message = getErrorMessage(value) if (ignoreErrors.some((ignore) => ignore.test(message))) { return } error.set(value) } const enabled = !!dsn && import.meta.env.PROD // No need to manually report these errors as they are automatically // captured by Sentry. And skip registering these in SSR. We can't use // onMounted because it's not available outside component lifecycle. if (typeof document !== 'undefined') { addEventListener('error', (event) => { set(event.error) }) addEventListener('unhandledrejection', (event) => { set(event.reason) }) } if (enabled) { Sentry.init({ dsn, environment, ignoreErrors }) getActivePinia()?.use(createSentryPiniaPlugin()) } return function errorHandler( e: any, instance: ComponentPublicInstance | null = null, info: string = '' ) { if (enabled) { pauseTracking() let userValue = toValue(user) || null if (!userValue?.id && !userValue?.email && !userValue?.username) { userValue = null } const statusCode = getHttpStatusCode(e) if (!statusCode || statusCode <= 400 || statusCode >= 500) { // ^ report 400 too because it might be caused by a bug in the frontend code const $ = instance && instance.$ const metadata = $ && { componentName: formatComponentName($), lifecycleHook: info, trace: formatTrace($), propsData: $.props } setTimeout(() => { Sentry.withScope((scope) => { scope.setUser(userValue) scope.setContext('vue', metadata) Sentry.captureException(e) }) }) } resetTracking() } else { console.error(e) } set(e) } }