Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 418a8da

Browse files
committed
feat(exception): autotrack errors captured by Vue
1 parent 06e6f3b commit 418a8da

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Vue from 'vue'
2+
import VueAnalytics from '../../src'
3+
4+
window.ga = jest.fn()
5+
6+
const originalErrorHandler = jest.fn()
7+
Vue.config.errorHandler = originalErrorHandler
8+
9+
Vue.use(VueAnalytics, {
10+
id: 'UA-1234-5',
11+
autoTracking: {
12+
exception: true,
13+
},
14+
})
15+
16+
const renderError = new Error('render error')
17+
let $vm
18+
19+
beforeEach(() => {
20+
$vm = new Vue({
21+
created() {
22+
throw renderError
23+
}
24+
})
25+
$vm.$mount()
26+
})
27+
28+
it('should track Vue render error', () => {
29+
expect(window.ga).toBeCalledWith('send', 'exception', {
30+
exDescription: 'render error',
31+
exFatal: true
32+
})
33+
})
34+
35+
it('should preserve original error handler', () => {
36+
expect(originalErrorHandler).toBeCalledWith(renderError, $vm, 'created hook')
37+
})

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ga from 'directives/ga'
77

88
// Features
99
import event from 'lib/event'
10-
import exception from 'lib/exception'
10+
import exception, { setupErrorHandler } from 'lib/exception'
1111
import page from 'lib/page'
1212
import query from 'lib/query'
1313
import require from 'lib/require'
@@ -36,6 +36,8 @@ export default function install (Vue, options = {}) {
3636
commands: config.commands
3737
}
3838

39+
setupErrorHandler(Vue)
40+
3941
bootstrap()
4042
}
4143

src/lib/exception.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ export default function exception (error, fatal = false) {
88
})
99
}
1010

11+
export function setupErrorHandler(Vue) {
12+
if (config.autoTracking.exception) {
13+
const originalErrorHandler = Vue.config.errorHandler
14+
Vue.config.errorHandler = function (error, vm, info) {
15+
vm.$ga.exception(error.message || error, true)
16+
if (typeof originalErrorHandler === 'function') {
17+
originalErrorHandler.call(this, error, vm, info)
18+
}
19+
}
20+
}
21+
}
22+
1123
export function autotracking () {
1224
if (!config.autoTracking.exception) {
1325
return

0 commit comments

Comments
 (0)