Просмотр исходного кода

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
DSha 7 лет назад
Родитель
Сommit
2f3020e9cc
2 измененных файлов с 26 добавлено и 1 удалено
  1. 3 1
      src/core/util/error.js
  2. 23 0
      test/unit/modules/util/invoke-with-error-handling.spec.js

+ 3 - 1
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)

+ 23 - 0
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()
+      })
+    })
+  }
+})