Skip to content

Commit 7186940

Browse files
committed
fix: should not swallow user catch on rejected promise in methods
fix #9694
1 parent 8082d2f commit 7186940

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/core/util/error.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ export function invokeWithErrorHandling (
4343
let res
4444
try {
4545
res = args ? handler.apply(context, args) : handler.call(context)
46-
if (res && !res._isVue && isPromise(res)) {
46+
if (res && !res._isVue && isPromise(res) && !res._handled) {
47+
res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
4748
// issue #9511
48-
// reassign to res to avoid catch triggering multiple times when nested calls
49-
res = res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
49+
// avoid catch triggering multiple times when nested calls
50+
res._handled = true
5051
}
5152
} catch (e) {
5253
handleError(e, vm, info)

test/unit/modules/util/invoke-with-error-handling.spec.js test/unit/modules/util/error.spec.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ describe('invokeWithErrorHandling', () => {
66
it('should errorHandler call once when nested calls return rejected promise', done => {
77
const originalHandler = Vue.config.errorHandler
88
const handler = Vue.config.errorHandler = jasmine.createSpy()
9+
const userCatch = jasmine.createSpy()
10+
const err = new Error('fake error')
911

1012
invokeWithErrorHandling(() => {
1113
return invokeWithErrorHandling(() => {
12-
return Promise.reject(new Error('fake error'))
14+
return Promise.reject(err)
1315
})
14-
}).then(() => {
16+
}).catch(userCatch).then(() => {
1517
Vue.config.errorHandler = originalHandler
1618
expect(handler.calls.count()).toBe(1)
19+
expect(userCatch).toHaveBeenCalledWith(err)
1720
done()
1821
})
1922
})

0 commit comments

Comments
 (0)