Skip to content

Commit 4bd15d4

Browse files
ankurk91kamilogorek
authored andcommitted
Add lifecycleHook argument in vue plugin handler
Vue.js v2.2.0 also captures errors in component lifecycle hooks https://vuejs.org/v2/api/#errorHandler
1 parent 50d0705 commit 4bd15d4

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

plugins/vue.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ function vuePlugin(Raven, Vue) {
2222
if (!Vue || !Vue.config) return;
2323

2424
var _oldOnError = Vue.config.errorHandler;
25-
Vue.config.errorHandler = function VueErrorHandler(error, vm) {
25+
Vue.config.errorHandler = function VueErrorHandler(error, vm, info) {
26+
var metaData = {
27+
componentName: formatComponentName(vm),
28+
propsData: vm.$options.propsData
29+
};
30+
31+
// lifecycleHook is not always available
32+
if (typeof info !== 'undefined') {
33+
metaData.lifecycleHook = info;
34+
}
35+
2636
Raven.captureException(error, {
27-
extra: {
28-
componentName: formatComponentName(vm),
29-
propsData: vm.$options.propsData
30-
}
37+
extra: metaData
3138
});
3239

3340
if (typeof _oldOnError === 'function') {
34-
_oldOnError.call(this, error, vm);
41+
_oldOnError.call(this, error, vm, info);
3542
}
3643
};
3744
}

test/plugins/vue.test.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@ describe('Vue plugin', function() {
1919
it('should capture component name and propsData', function() {
2020
vuePlugin(Raven, this.MockVue);
2121

22-
this.MockVue.config.errorHandler(
23-
new Error('foo'),
24-
{
25-
$options: {
26-
propsData: {
27-
foo: 'bar'
28-
}
22+
this.MockVue.config.errorHandler(new Error('foo'), {
23+
$options: {
24+
propsData: {
25+
foo: 'bar'
2926
}
30-
},
31-
{} /* vm */
32-
);
27+
}
28+
});
3329

3430
assert.isTrue(Raven.captureException.calledOnce);
3531

@@ -57,5 +53,27 @@ describe('Vue plugin', function() {
5753
assert.equal(errorHandler.args[0][0], err);
5854
assert.equal(errorHandler.args[0][1], vm);
5955
});
56+
57+
it('should capture lifecycle hook', function() {
58+
vuePlugin(Raven, this.MockVue);
59+
60+
this.MockVue.config.errorHandler(
61+
new Error('foo'),
62+
{
63+
$options: {
64+
propsData: {}
65+
}
66+
},
67+
'mounted'
68+
);
69+
70+
assert.isTrue(Raven.captureException.calledOnce);
71+
72+
assert.deepEqual(Raven.captureException.args[0][1].extra, {
73+
propsData: {},
74+
componentName: 'anonymous component',
75+
lifecycleHook: 'mounted'
76+
});
77+
});
6078
});
6179
});

0 commit comments

Comments
 (0)