]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: renderError
authorEvan You <yyx990803@gmail.com>
Mon, 24 Sep 2018 00:30:26 +0000 (20:30 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 24 Sep 2018 00:30:26 +0000 (20:30 -0400)
packages/core/src/component.ts
packages/core/src/componentUtils.ts
packages/core/src/errorHandling.ts

index 982faa858331679dab981225afef3529962ec32d..3ac07bde4feafda93cc77b95ec1795919deed037 100644 (file)
@@ -34,6 +34,7 @@ export interface MountedComponent<D = Data, P = Data> extends Component {
   $options: ComponentOptions<D, P>
 
   render: RenderFunction<P>
+  renderError?: (e: Error) => any
   data?(): Partial<D>
   beforeCreate?(): void
   created?(): void
index 0a4091193962ba61ce23d78aee8b1682e313fcef..093e5800811707def3c054ee317a71a565661e14 100644 (file)
@@ -14,6 +14,7 @@ import {
 import { initializeWatch, teardownWatch } from './componentWatch'
 import { Data, ComponentOptions } from './componentOptions'
 import { createRenderProxy } from './componentProxy'
+import { handleError, ErrorTypes } from './errorHandling'
 
 export function createComponentInstance(
   vnode: VNode,
@@ -53,11 +54,24 @@ export function createComponentInstance(
 }
 
 export function renderInstanceRoot(instance: MountedComponent) {
-  // TODO handle render error
-  return normalizeComponentRoot(
-    instance.render.call(instance.$proxy, instance.$props, instance.$slots),
-    instance.$parentVNode
-  )
+  let vnode
+  try {
+    vnode = instance.render.call(
+      instance.$proxy,
+      instance.$props,
+      instance.$slots
+    )
+  } catch (e1) {
+    handleError(e1, instance, ErrorTypes.RENDER)
+    if (__DEV__ && instance.renderError) {
+      try {
+        vnode = instance.renderError.call(instance.$proxy, e1)
+      } catch (e2) {
+        handleError(e2, instance, ErrorTypes.RENDER_ERROR)
+      }
+    }
+  }
+  return normalizeComponentRoot(vnode, instance.$parentVNode)
 }
 
 export function teardownComponentInstance(instance: MountedComponent) {
index 7714bcf45fc3e5edb80b5cdda59068b44c7c7ff0..8a59d2ab0d5dcdafe6a275df23418beb712de170 100644 (file)
@@ -2,9 +2,10 @@ import { MountedComponent } from './component'
 
 export const enum ErrorTypes {
   LIFECYCLE = 1,
-  RENDER = 2,
-  NATIVE_EVENT_HANDLER = 3,
-  COMPONENT_EVENT_HANDLER = 4
+  RENDER,
+  RENDER_ERROR,
+  NATIVE_EVENT_HANDLER,
+  COMPONENT_EVENT_HANDLER
 }
 
 const globalHandlers: Function[] = []
@@ -19,8 +20,7 @@ export function globalHandleError(handler: () => void) {
 export function handleError(
   err: Error,
   instance: MountedComponent,
-  type: ErrorTypes,
-  code: number
+  type: ErrorTypes
 ) {
   // TODO
 }