Skip to content

Commit 5b8650a

Browse files
committed
fix: Vue.config.errorHandler should capture rejected promised in watchers(vuejs#10009)
1 parent 875d6ac commit 5b8650a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/core/observer/watcher.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
parsePath,
88
_Set as Set,
99
handleError,
10+
invokeWithErrorHandling,
1011
noop
1112
} from '../util/index'
1213

@@ -191,11 +192,7 @@ export default class Watcher {
191192
const oldValue = this.value
192193
this.value = value
193194
if (this.user) {
194-
try {
195-
this.cb.call(this.vm, value, oldValue)
196-
} catch (e) {
197-
handleError(e, this.vm, `callback for watcher "${this.expression}"`)
198-
}
195+
invokeWithErrorHandling(this.cb, this.vm, [value, oldValue], this.vm, ("callback for watcher \"" + (this.expression) + "\""))
199196
} else {
200197
this.cb.call(this.vm, value, oldValue)
201198
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ describe('Error handling', () => {
138138
}).then(done)
139139
})
140140

141+
it('should recover from errors in user watcher from promise error', done => {
142+
const vm = createTestInstance(components.userWatcherPromise)
143+
vm.n++
144+
setTimeout(() => {
145+
expect(`Error in callback for watcher "n" (Promise/async)`).toHaveBeenWarned()
146+
expect(`Error: userWatcherPromise error`).toHaveBeenWarned()
147+
assertBothInstancesActive(vm).then(done)
148+
})
149+
})
150+
141151
it('should recover from errors in user immediate watcher callback', done => {
142152
const vm = createTestInstance(components.userImmediateWatcherCallback)
143153
waitForUpdate(() => {
@@ -344,6 +354,18 @@ function createErrorTestComponents () {
344354
}
345355
}
346356

357+
components.userWatcherPromise = {
358+
props: ['n'],
359+
watch: {
360+
n () {
361+
return new Promise((resolve, reject) => reject(new Error('userWatcherPromise error')))
362+
}
363+
},
364+
render (h) {
365+
return h('div', this.n)
366+
}
367+
}
368+
347369
components.userImmediateWatcherCallback = {
348370
props: ['n'],
349371
watch: {

0 commit comments

Comments
 (0)