Skip to content

Commit 10a675f

Browse files
committed
Add Vue.js (2.0) plugin
1 parent 2210615 commit 10a675f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

plugins/vue.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Vue.js 2.0 plugin
3+
*
4+
*/
5+
'use strict';
6+
7+
function vuePlugin(Raven, Vue) {
8+
Vue = Vue || window.Vue;
9+
10+
// quit if Vue isn't on the page
11+
if (!Vue || !Vue.config) return;
12+
13+
var _oldOnError = Vue.config.errorHandler;
14+
Vue.config.errorHandler = function VueErrorHandler(error, vm) {
15+
Raven.captureException(error, {
16+
extra: {
17+
componentName: Vue.util.formatComponentName(vm),
18+
propsData: vm.$options.propsData
19+
}
20+
});
21+
22+
if (typeof _oldOnError === 'function') {
23+
_oldOnError.call(this, error, vm);
24+
}
25+
};
26+
}
27+
28+
module.exports = vuePlugin;
29+

test/plugins/vue.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var _Raven = require('../../src/raven');
2+
var vuePlugin = require('../../plugins/vue');
3+
4+
var Raven;
5+
describe('Vue plugin', function () {
6+
beforeEach(function () {
7+
Raven = new _Raven();
8+
Raven.config('http://[email protected]:80/2');
9+
});
10+
11+
describe('errorHandler', function () {
12+
beforeEach(function () {
13+
this.sinon.stub(Raven, 'captureException');
14+
this.MockVue = {
15+
config: {},
16+
util: {
17+
formatComponentName: function() {
18+
return '<root component>'
19+
}
20+
}
21+
};
22+
});
23+
24+
it('should capture component name and propsData', function () {
25+
vuePlugin(Raven, this.MockVue);
26+
27+
this.MockVue.config.errorHandler(new Error('foo'), {
28+
$options: {
29+
propsData: {
30+
foo: 'bar'
31+
}
32+
}
33+
}, {} /* vm */);
34+
35+
assert.isTrue(Raven.captureException.calledOnce);
36+
37+
assert.deepEqual(Raven.captureException.args[0][1].extra, {
38+
propsData: {
39+
foo: 'bar'
40+
},
41+
componentName: '<root component>'
42+
});
43+
});
44+
45+
it('should call the existing error handler', function () {
46+
var errorHandler = this.sinon.stub();
47+
this.MockVue.config.errorHandler = errorHandler;
48+
vuePlugin(Raven, this.MockVue); // should override errorHandler
49+
50+
var err = new Error('foo');
51+
var vm = {
52+
$options: { propsData: {} }
53+
};
54+
this.MockVue.config.errorHandler(err, vm);
55+
56+
assert.isTrue(Raven.captureException.calledOnce);
57+
assert.isTrue(errorHandler.calledOnce);
58+
assert.equal(errorHandler.args[0][0], err);
59+
assert.equal(errorHandler.args[0][1], vm);
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)