Skip to content

Commit 76d7809

Browse files
committed
config.errorHandler should capture user wathcer errors too (ref #3142)
1 parent 194b20a commit 76d7809

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/core/observer/watcher.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,24 @@ export default class Watcher {
188188
// set new value
189189
const oldValue = this.value
190190
this.value = value
191-
this.cb.call(this.vm, value, oldValue)
191+
if (this.user) {
192+
try {
193+
this.cb.call(this.vm, value, oldValue)
194+
} catch (e) {
195+
process.env.NODE_ENV !== 'production' && warn(
196+
`Error in watcher "${this.expression}"`,
197+
this.vm
198+
)
199+
/* istanbul ignore else */
200+
if (config.errorHandler) {
201+
config.errorHandler.call(null, e, this.vm)
202+
} else {
203+
throw e
204+
}
205+
}
206+
} else {
207+
this.cb.call(this.vm, value, oldValue)
208+
}
192209
}
193210
}
194211
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ describe('Global config', () => {
3333
expect(spy).toHaveBeenCalledWith(err, vm)
3434
Vue.config.errorHandler = null
3535
})
36+
37+
it('should capture user watcher callback errors', done => {
38+
const spy = jasmine.createSpy('errorHandler')
39+
Vue.config.errorHandler = spy
40+
const err = new Error()
41+
const vm = new Vue({
42+
data: { a: 1 },
43+
watch: {
44+
a: () => {
45+
throw err
46+
}
47+
}
48+
}).$mount()
49+
vm.a = 2
50+
waitForUpdate(() => {
51+
expect(spy).toHaveBeenCalledWith(err, vm)
52+
Vue.config.errorHandler = null
53+
}).then(done)
54+
})
3655
})
3756

3857
describe('optionMergeStrategies', () => {

0 commit comments

Comments
 (0)