Skip to content

Commit fbadeb8

Browse files
authored
fix(vue): Make options parameter optional on attachErrorHandler (#18072)
Looks like our Vue documentation for how to [dynamically attach an errorHandler](https://docs.sentry.io/platforms/javascript/guides/vue/features/multiple-apps/#dynamic-initialization) to a Vue app advertised that `attachErrorHandler` can be called without having to specify options. Intuitively, this makes sense to me since the only option we actually read is `attachProps`. We can instead default this to true (just like in the main Vue init option) and make the `options` parameter optional. For v11, we should consider gating prop data attachment behind `sendDefaultPii`. I added a TODO to the code. closes #18060
1 parent 94190f8 commit fbadeb8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/vue/src/errorhandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { formatComponentName, generateComponentTrace } from './vendor/components
44

55
type UnknownFunc = (...args: unknown[]) => void;
66

7-
export const attachErrorHandler = (app: Vue, options: VueOptions): void => {
7+
export const attachErrorHandler = (app: Vue, options?: Partial<VueOptions>): void => {
88
const { errorHandler: originalErrorHandler } = app.config;
99

1010
app.config.errorHandler = (error: Error, vm: ViewModel, lifecycleHook: string): void => {
@@ -16,7 +16,8 @@ export const attachErrorHandler = (app: Vue, options: VueOptions): void => {
1616
trace,
1717
};
1818

19-
if (options.attachProps && vm) {
19+
// TODO(v11): guard via sendDefaultPii?
20+
if (options?.attachProps !== false && vm) {
2021
// Vue2 - $options.propsData
2122
// Vue3 - $props
2223
if (vm.$options?.propsData) {

packages/vue/test/errorHandler.test.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ describe('attachErrorHandler', () => {
112112
// assert
113113
t.expect.errorToHaveBeenCaptured().withProps(props);
114114
});
115+
116+
test('`propsData` is added, if no options are provided to `attachErrorHandler`', () => {
117+
// arrange
118+
const props = { stubProp: 'stubData' };
119+
const t = testHarness({
120+
vm: {
121+
$props: props,
122+
},
123+
optionsUndefined: true,
124+
});
125+
126+
// act
127+
vi.useFakeTimers();
128+
expect(() => t.run()).toThrow(DummyError);
129+
vi.runAllTimers();
130+
131+
// assert
132+
t.expect.errorToHaveBeenCaptured().withProps(props);
133+
});
115134
});
116135

117136
describe('and `vm.$props` is defined', () => {
@@ -220,6 +239,7 @@ type TestHarnessOpts = {
220239
enableConsole?: boolean;
221240
silent?: boolean;
222241
attachProps?: boolean;
242+
optionsUndefined?: boolean;
223243
};
224244

225245
class DummyError extends Error {
@@ -236,6 +256,7 @@ const testHarness = ({
236256
enableErrorHandler,
237257
enableConsole,
238258
vm,
259+
optionsUndefined = false,
239260
}: TestHarnessOpts) => {
240261
vi.useFakeTimers();
241262
const providedErrorHandlerSpy = vi.fn();
@@ -274,13 +295,15 @@ const testHarness = ({
274295
}
275296
/* eslint-enable no-global-assign */
276297

277-
const options: Options = {
278-
attachProps: !!attachProps,
279-
tracingOptions: {},
280-
trackComponents: [],
281-
timeout: 0,
282-
hooks: [] as Operation[],
283-
};
298+
const options: Options | undefined = optionsUndefined
299+
? undefined
300+
: {
301+
attachProps: !!attachProps,
302+
tracingOptions: {},
303+
trackComponents: [],
304+
timeout: 0,
305+
hooks: [] as Operation[],
306+
};
284307

285308
return {
286309
run: () => {

0 commit comments

Comments
 (0)