Skip to content

Commit

Permalink
fix(#9511): avoid promise catch multiple times (#9526)
Browse files Browse the repository at this point in the history
* fix(#9511): avoid promise catch multiple times

* fix(#9511): add a test case for util/error/invokeWithErrorHandling

* fix(#9511): update test case for util/error/invokeWithErrorHandling
  • Loading branch information
shasharoman authored and yyx990803 committed Feb 21, 2019
1 parent 8a80a23 commit 2f3020e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/util/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function invokeWithErrorHandling (
try {
res = args ? handler.apply(context, args) : handler.call(context)
if (res && !res._isVue && isPromise(res)) {
res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
// issue #9511
// reassign to res to avoid catch triggering multiple times when nested calls
res = res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
}
} catch (e) {
handleError(e, vm, info)
Expand Down
23 changes: 23 additions & 0 deletions test/unit/modules/util/invoke-with-error-handling.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Vue from 'vue'
import { invokeWithErrorHandling } from 'core/util/error'

describe('invokeWithErrorHandling', () => {
if (typeof Promise !== 'undefined') {
it('should errorHandler call once when nested calls return rejected promise', done => {
let times = 0

Vue.config.errorHandler = function () {
times++
}

invokeWithErrorHandling(() => {
return invokeWithErrorHandling(() => {
return Promise.reject(new Error('fake error'))
})
}).then(() => {
expect(times).toBe(1)
done()
})
})
}
})

0 comments on commit 2f3020e

Please sign in to comment.