Skip to content

Commit 2686818

Browse files
afontcuyyx990803
authored andcommitted
fix(error handling): handle errors on immediate watcher execution (#8581)
The handle callback call should be wrapped in a try/catch that explicitly calls handleError fix #8567
1 parent 7b7164c commit 2686818

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/core/instance/state.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ export function stateMixin (Vue: Class<Component>) {
351351
options.user = true
352352
const watcher = new Watcher(vm, expOrFn, cb, options)
353353
if (options.immediate) {
354-
cb.call(vm, watcher.value)
354+
try {
355+
cb.call(vm, watcher.value)
356+
} catch (error) {
357+
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
358+
}
355359
}
356360
return function unwatchFn () {
357361
watcher.teardown()

test/unit/features/error-handling.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ describe('Error handling', () => {
9292
}).then(done)
9393
})
9494

95+
it('should recover from errors in user immediate watcher callback', done => {
96+
const vm = createTestInstance(components.userImmediateWatcherCallback)
97+
waitForUpdate(() => {
98+
expect(`Error in callback for immediate watcher "n"`).toHaveBeenWarned()
99+
expect(`Error: userImmediateWatcherCallback error`).toHaveBeenWarned()
100+
}).thenWaitFor(next => {
101+
assertBothInstancesActive(vm).end(next)
102+
}).then(done)
103+
})
104+
95105
it('config.errorHandler should capture render errors', done => {
96106
const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
97107
const vm = createTestInstance(components.render)
@@ -234,6 +244,21 @@ function createErrorTestComponents () {
234244
}
235245
}
236246

247+
components.userImmediateWatcherCallback = {
248+
props: ['n'],
249+
watch: {
250+
n: {
251+
immediate: true,
252+
handler () {
253+
throw new Error('userImmediateWatcherCallback error')
254+
}
255+
}
256+
},
257+
render (h) {
258+
return h('div', this.n)
259+
}
260+
}
261+
237262
// event errors
238263
components.event = {
239264
beforeCreate () {

0 commit comments

Comments
 (0)