瀏覽代碼

feat(runtime-vapor): add app-level error handler (#160)

Co-authored-by: Kevin Deng 三咲智子 <sxzz@sxzz.moe>
Doctor Wu 2 年之前
父節點
當前提交
bd888b9b1e
共有 1 個文件被更改,包括 17 次插入12 次删除
  1. 17 12
      packages/runtime-vapor/src/errorHandling.ts

+ 17 - 12
packages/runtime-vapor/src/errorHandling.ts

@@ -7,7 +7,11 @@ import type { ComponentInternalInstance } from './component'
 import { isFunction, isPromise } from '@vue/shared'
 import { warn } from './warning'
 import { VaporLifecycleHooks } from './apiLifecycle'
-import { BaseWatchErrorCodes } from '@vue/reactivity'
+import {
+  BaseWatchErrorCodes,
+  pauseTracking,
+  resetTracking,
+} from '@vue/reactivity'
 
 // contexts where user provided function may be executed, in addition to
 // lifecycle hooks.
@@ -131,18 +135,19 @@ export function handleError(
       cur = cur.parent
     }
 
-    // TODO: need appContext interface
     // app-level handling
-    // const appErrorHandler = instance.appContext?.config.errorHandler
-    // if (appErrorHandler) {
-    //   callWithErrorHandling(
-    //     appErrorHandler,
-    //     null,
-    //     ErrorCodes.APP_ERROR_HANDLER,
-    //     [err, exposedInstance, errorInfo],
-    //   )
-    //   return
-    // }
+    const appErrorHandler = instance.appContext.config.errorHandler
+    if (appErrorHandler) {
+      pauseTracking()
+      callWithErrorHandling(
+        appErrorHandler,
+        null,
+        VaporErrorCodes.APP_ERROR_HANDLER,
+        [err, instance, errorInfo],
+      )
+      resetTracking()
+      return
+    }
   }
   logError(err, type, throwInDev)
 }