Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #77 from nekitk/automatic-vue-error-tracking
Browse files Browse the repository at this point in the history
Automatically track errors captured by Vue
  • Loading branch information
MatteoGabriele authored Dec 26, 2017
2 parents bd8fdd2 + 418a8da commit 147a499
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
37 changes: 37 additions & 0 deletions __tests__/lib/autotracking.exception.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Vue from 'vue'
import VueAnalytics from '../../src'

window.ga = jest.fn()

const originalErrorHandler = jest.fn()
Vue.config.errorHandler = originalErrorHandler

Vue.use(VueAnalytics, {
id: 'UA-1234-5',
autoTracking: {
exception: true,
},
})

const renderError = new Error('render error')
let $vm

beforeEach(() => {
$vm = new Vue({
created() {
throw renderError
}
})
$vm.$mount()
})

it('should track Vue render error', () => {
expect(window.ga).toBeCalledWith('send', 'exception', {
exDescription: 'render error',
exFatal: true
})
})

it('should preserve original error handler', () => {
expect(originalErrorHandler).toBeCalledWith(renderError, $vm, 'created hook')
})
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ga from 'directives/ga'

// Features
import event from 'lib/event'
import exception from 'lib/exception'
import exception, { setupErrorHandler } from 'lib/exception'
import page from 'lib/page'
import query from 'lib/query'
import require from 'lib/require'
Expand Down Expand Up @@ -36,6 +36,8 @@ export default function install (Vue, options = {}) {
commands: config.commands
}

setupErrorHandler(Vue)

bootstrap()
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export default function exception (error, fatal = false) {
})
}

export function setupErrorHandler(Vue) {
if (config.autoTracking.exception) {
const originalErrorHandler = Vue.config.errorHandler
Vue.config.errorHandler = function (error, vm, info) {
vm.$ga.exception(error.message || error, true)
if (typeof originalErrorHandler === 'function') {
originalErrorHandler.call(this, error, vm, info)
}
}
}
}

export function autotracking () {
if (!config.autoTracking.exception) {
return
Expand Down

0 comments on commit 147a499

Please sign in to comment.