From 2f3020e9cc1ad5c878606b56bb73a30b1d9bb7d9 Mon Sep 17 00:00:00 2001 From: DSha Date: Fri, 22 Feb 2019 03:01:53 +0800 Subject: [PATCH] fix(#9511): avoid promise catch multiple times (#9526) * 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 --- src/core/util/error.js | 4 +++- .../util/invoke-with-error-handling.spec.js | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/unit/modules/util/invoke-with-error-handling.spec.js diff --git a/src/core/util/error.js b/src/core/util/error.js index dffd8d84c26..8b8b9014ddb 100644 --- a/src/core/util/error.js +++ b/src/core/util/error.js @@ -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) diff --git a/test/unit/modules/util/invoke-with-error-handling.spec.js b/test/unit/modules/util/invoke-with-error-handling.spec.js new file mode 100644 index 00000000000..e1c95e02b35 --- /dev/null +++ b/test/unit/modules/util/invoke-with-error-handling.spec.js @@ -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() + }) + }) + } +})