Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#8567): handle errors on immediate watchers #8581

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call watcher.getAndInvoke @yyx990803 ?

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