-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(vue): Export Vue exception capture helper #24586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f52f52b
9d9ee85
6a0cc00
9e71e16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,34 +4,31 @@ import { formatComponentName, generateComponentTrace } from './vendor/components | |
|
|
||
| type UnknownFunc = (...args: unknown[]) => void; | ||
|
|
||
| /** | ||
| * Captures an exception with Vue component metadata. | ||
| * | ||
| * This can be used from a Vue `onErrorCaptured` hook when the automatic error handler is disabled or when an error | ||
| * boundary stops the error from propagating to the application-level handler. | ||
| * | ||
| * @param handled - Whether the boundary stops the error from propagating. | ||
| */ | ||
| export const captureVueException = ( | ||
| error: unknown, | ||
| vm: ViewModel | null, | ||
| lifecycleHook: string, | ||
| handled: boolean, | ||
| options?: Partial<VueOptions>, | ||
| ): void => { | ||
| captureVueExceptionWithMechanism(error, vm, lifecycleHook, handled, options); | ||
| }; | ||
|
|
||
| export const attachErrorHandler = (app: Vue, options?: Partial<VueOptions>): void => { | ||
| const { errorHandler: originalErrorHandler } = app.config; | ||
|
|
||
|
vansh-nagar marked this conversation as resolved.
|
||
| app.config.errorHandler = (error: Error, vm: ViewModel, lifecycleHook: string): void => { | ||
| const componentName = formatComponentName(vm, false); | ||
| const trace = vm ? generateComponentTrace(vm) : ''; | ||
| const metadata: Record<string, unknown> = { | ||
| componentName, | ||
| lifecycleHook, | ||
| trace, | ||
| }; | ||
|
|
||
| if (options?.attachProps !== false && vm) { | ||
| // Vue2 - $options.propsData | ||
| // Vue3 - $props | ||
| if (vm.$options?.propsData) { | ||
| metadata.propsData = vm.$options.propsData; | ||
| } else if (vm.$props) { | ||
| metadata.propsData = vm.$props; | ||
| } | ||
| } | ||
|
|
||
| app.config.errorHandler = (error: unknown, vm: ViewModel | null, lifecycleHook: string): void => { | ||
| // Capture exception in the next event loop, to make sure that all breadcrumbs are recorded in time. | ||
| setTimeout(() => { | ||
| captureException(error, { | ||
| captureContext: { contexts: { vue: metadata } }, | ||
| mechanism: { handled: !!originalErrorHandler, type: 'auto.function.vue.error_handler' }, | ||
| }); | ||
| captureVueExceptionWithMechanism(error, vm, lifecycleHook, !!originalErrorHandler, options); | ||
| }); | ||
|
Comment on lines
29
to
32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Metadata computation is now deferred via Suggested FixCapture the component metadata synchronously when the error is first handled, before scheduling the Prompt for AI Agent |
||
|
|
||
| // Check if the current `app.config.errorHandler` is explicitly set by the user before calling it. | ||
|
|
@@ -42,3 +39,34 @@ export const attachErrorHandler = (app: Vue, options?: Partial<VueOptions>): voi | |
| } | ||
| }; | ||
| }; | ||
|
|
||
| function captureVueExceptionWithMechanism( | ||
| error: unknown, | ||
| vm: ViewModel | null, | ||
| lifecycleHook: string, | ||
| handled: boolean, | ||
| options?: Partial<VueOptions>, | ||
| ): void { | ||
| const componentName = formatComponentName(vm || undefined, false); | ||
| const trace = vm ? generateComponentTrace(vm) : ''; | ||
| const metadata: Record<string, unknown> = { | ||
| componentName, | ||
| lifecycleHook, | ||
| trace, | ||
| }; | ||
|
|
||
| if (options?.attachProps !== false && vm) { | ||
| // Vue2 - $options.propsData | ||
| // Vue3 - $props | ||
| if (vm.$options?.propsData) { | ||
| metadata.propsData = vm.$options.propsData; | ||
| } else if (vm.$props) { | ||
| metadata.propsData = vm.$props; | ||
| } | ||
| } | ||
|
|
||
| captureException(error, { | ||
| captureContext: { contexts: { vue: metadata } }, | ||
| mechanism: { handled, type: 'auto.function.vue.error_handler' }, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import type { Scope } from '@sentry/core'; | ||
| import { setCurrentClient } from '@sentry/browser'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { createApp, defineComponent, h, onErrorCaptured } from 'vue'; | ||
| import { captureVueException, withScope } from '../../src'; | ||
|
|
||
| describe('captureVueException', () => { | ||
| afterEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it('captures a Vue error boundary exception with its local scope', () => { | ||
| const error = 'render failed'; | ||
| const captureException = vi.fn((_error: unknown, _hint: unknown, scope?: Scope) => { | ||
| expect(scope?.getScopeData().tags).toEqual({ boundary: 'checkout' }); | ||
| }); | ||
| setCurrentClient({ captureException } as any); | ||
|
|
||
| const child = defineComponent({ | ||
| name: 'Checkout', | ||
| setup() { | ||
| throw error; | ||
| }, | ||
| render: () => h('div'), | ||
| }); | ||
| const boundary = defineComponent({ | ||
| name: 'ErrorBoundary', | ||
| setup() { | ||
| onErrorCaptured((caughtError, instance, info) => { | ||
| withScope(scope => { | ||
| scope.setTag('boundary', 'checkout'); | ||
| captureVueException(caughtError, instance, info, true); | ||
| }); | ||
|
|
||
| expect(captureException).toHaveBeenCalledTimes(1); | ||
| return false; | ||
| }); | ||
|
|
||
| return () => h(child); | ||
| }, | ||
| }); | ||
| const app = createApp(boundary); | ||
|
|
||
| app.mount(document.createElement('div')); | ||
|
|
||
| expect(captureException).toHaveBeenCalledWith( | ||
| error, | ||
| expect.objectContaining({ | ||
| captureContext: { | ||
| contexts: { | ||
| vue: expect.objectContaining({ | ||
| componentName: '<Checkout>', | ||
| lifecycleHook: 'setup function', | ||
| }), | ||
| }, | ||
| }, | ||
| mechanism: { handled: true, type: 'auto.function.vue.error_handler' }, | ||
| }), | ||
| expect.anything(), | ||
| ); | ||
| app.unmount(); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.