config.errorHandler should capture user wathcer errors too (ref #3142)

This commit is contained in:
Evan You 2016-06-22 22:43:58 -04:00
parent 194b20a9dc
commit 76d7809a11
2 changed files with 37 additions and 1 deletions

View File

@ -188,7 +188,24 @@ export default class Watcher {
// set new value
const oldValue = this.value
this.value = value
this.cb.call(this.vm, value, oldValue)
if (this.user) {
try {
this.cb.call(this.vm, value, oldValue)
} catch (e) {
process.env.NODE_ENV !== 'production' && warn(
`Error in watcher "${this.expression}"`,
this.vm
)
/* istanbul ignore else */
if (config.errorHandler) {
config.errorHandler.call(null, e, this.vm)
} else {
throw e
}
}
} else {
this.cb.call(this.vm, value, oldValue)
}
}
}
}

View File

@ -33,6 +33,25 @@ describe('Global config', () => {
expect(spy).toHaveBeenCalledWith(err, vm)
Vue.config.errorHandler = null
})
it('should capture user watcher callback errors', done => {
const spy = jasmine.createSpy('errorHandler')
Vue.config.errorHandler = spy
const err = new Error()
const vm = new Vue({
data: { a: 1 },
watch: {
a: () => {
throw err
}
}
}).$mount()
vm.a = 2
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(err, vm)
Vue.config.errorHandler = null
}).then(done)
})
})
describe('optionMergeStrategies', () => {