From c531319f3bf3dab75ad52a0ccabf875a8d7fe43f Mon Sep 17 00:00:00 2001 From: shasharoman Date: Tue, 19 Feb 2019 18:09:10 +0800 Subject: [PATCH 1/3] fix(#9511): avoid promise catch multiple times --- src/core/util/error.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/util/error.js b/src/core/util/error.js index e4604ecf230..93dc1e2e49c 100644 --- a/src/core/util/error.js +++ b/src/core/util/error.js @@ -36,7 +36,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) From 749a50308061c8d4b8d06dc6bf5be5a4c766f2d4 Mon Sep 17 00:00:00 2001 From: shasharoman Date: Tue, 19 Feb 2019 19:12:01 +0800 Subject: [PATCH 2/3] fix(#9511): add a test case for util/error/invokeWithErrorHandling --- .../util/invoke-with-error-handling.spec.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/unit/modules/util/invoke-with-error-handling.spec.js 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..8c6dd9ae7dc --- /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('nested calls do not trigger multiple errorHandler calls', done => { + let times = 0 + + Vue.config.errorHandler = function () { + times++ + } + + invokeWithErrorHandling(() => { + return invokeWithErrorHandling(() => { + return Promise.reject(new Error('fake error')) + }) + }).catch(() => { + expect(times).toBe(1) + done() + }) + }) + } +}) From a7bd451be285eb86c8b2354912e430421341f835 Mon Sep 17 00:00:00 2001 From: shasharoman Date: Tue, 19 Feb 2019 19:33:10 +0800 Subject: [PATCH 3/3] fix(#9511): update test case for util/error/invokeWithErrorHandling --- test/unit/modules/util/invoke-with-error-handling.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/modules/util/invoke-with-error-handling.spec.js b/test/unit/modules/util/invoke-with-error-handling.spec.js index 8c6dd9ae7dc..e1c95e02b35 100644 --- a/test/unit/modules/util/invoke-with-error-handling.spec.js +++ b/test/unit/modules/util/invoke-with-error-handling.spec.js @@ -3,7 +3,7 @@ import { invokeWithErrorHandling } from 'core/util/error' describe('invokeWithErrorHandling', () => { if (typeof Promise !== 'undefined') { - it('nested calls do not trigger multiple errorHandler calls', done => { + it('should errorHandler call once when nested calls return rejected promise', done => { let times = 0 Vue.config.errorHandler = function () { @@ -14,7 +14,7 @@ describe('invokeWithErrorHandling', () => { return invokeWithErrorHandling(() => { return Promise.reject(new Error('fake error')) }) - }).catch(() => { + }).then(() => { expect(times).toBe(1) done() })