Skip to content

Commit

Permalink
fix(error handling): handle errors on immediate watcher execution (#8581
Browse files Browse the repository at this point in the history
)

The handle callback call should be wrapped in a try/catch that explicitly calls handleError

fix #8567
  • Loading branch information
afontcu authored and yyx990803 committed Nov 29, 2018
1 parent 7b7164c commit 2686818
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ export function stateMixin (Vue: Class<Component>) {
options.user = true
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
cb.call(vm, watcher.value)
try {
cb.call(vm, watcher.value)
} catch (error) {
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
}
}
return function unwatchFn () {
watcher.teardown()
Expand Down
25 changes: 25 additions & 0 deletions test/unit/features/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ describe('Error handling', () => {
}).then(done)
})

it('should recover from errors in user immediate watcher callback', done => {
const vm = createTestInstance(components.userImmediateWatcherCallback)
waitForUpdate(() => {
expect(`Error in callback for immediate watcher "n"`).toHaveBeenWarned()
expect(`Error: userImmediateWatcherCallback error`).toHaveBeenWarned()
}).thenWaitFor(next => {
assertBothInstancesActive(vm).end(next)
}).then(done)
})

it('config.errorHandler should capture render errors', done => {
const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
const vm = createTestInstance(components.render)
Expand Down Expand Up @@ -234,6 +244,21 @@ function createErrorTestComponents () {
}
}

components.userImmediateWatcherCallback = {
props: ['n'],
watch: {
n: {
immediate: true,
handler () {
throw new Error('userImmediateWatcherCallback error')
}
}
},
render (h) {
return h('div', this.n)
}
}

// event errors
components.event = {
beforeCreate () {
Expand Down

0 comments on commit 2686818

Please sign in to comment.