Browse Source

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

dev
Evan You 9 years ago
parent
commit
76d7809a11
  1. 19
      src/core/observer/watcher.js
  2. 19
      test/unit/features/global-api/config.spec.js

19
src/core/observer/watcher.js

@ -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)
}
}
}
}

19
test/unit/features/global-api/config.spec.js

@ -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', () => {

Loading…
Cancel
Save