Skip to content

Commit

Permalink
feat(vue): Mark errors caught by Vue wrappers as unhandled (#8905)
Browse files Browse the repository at this point in the history
Report exceptions caught by our Vue error handler and routing instrumentation as unhandled.

Detailed write up in #8890
  • Loading branch information
Lms24 authored Sep 4, 2023
1 parent 726a6ad commit fe375b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/vue/src/errorhandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getCurrentHub } from '@sentry/browser';
import { addExceptionMechanism } from '@sentry/utils';

import type { Options, ViewModel, Vue } from './types';
import { formatComponentName, generateComponentTrace } from './vendor/components';
Expand Down Expand Up @@ -31,6 +32,14 @@ export const attachErrorHandler = (app: Vue, options: Options): void => {
setTimeout(() => {
getCurrentHub().withScope(scope => {
scope.setContext('vue', metadata);

scope.addEventProcessor(event => {
addExceptionMechanism(event, {
handled: false,
});
return event;
});

getCurrentHub().captureException(error);
});
});
Expand Down
12 changes: 11 additions & 1 deletion packages/vue/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { captureException, WINDOW } from '@sentry/browser';
import type { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { addExceptionMechanism } from '@sentry/utils';

import { getActiveTransaction } from './tracing';

Expand Down Expand Up @@ -78,7 +79,16 @@ export function vueRouterInstrumentation(
});
}

router.onError(error => captureException(error));
router.onError(error =>
captureException(error, scope => {
scope.addEventProcessor(event => {
addExceptionMechanism(event, { handled: false });
return event;
});

return scope;
}),
);

router.beforeEach((to, from, next) => {
// According to docs we could use `from === VueRouter.START_LOCATION` but I couldnt get it working for Vue 2
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ describe('vueRouterInstrumentation()', () => {
onErrorCallback(testError);

expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
expect(captureExceptionSpy).toHaveBeenCalledWith(testError);
// second function is the scope callback
expect(captureExceptionSpy).toHaveBeenCalledWith(testError, expect.any(Function));
});

it.each([
Expand Down

0 comments on commit fe375b9

Please sign in to comment.