diff --git a/packages/core/src/component.ts b/packages/core/src/component.ts index 982faa85..3ac07bde 100644 --- a/packages/core/src/component.ts +++ b/packages/core/src/component.ts @@ -34,6 +34,7 @@ export interface MountedComponent extends Component { $options: ComponentOptions render: RenderFunction

+ renderError?: (e: Error) => any data?(): Partial beforeCreate?(): void created?(): void diff --git a/packages/core/src/componentUtils.ts b/packages/core/src/componentUtils.ts index 0a409119..093e5800 100644 --- a/packages/core/src/componentUtils.ts +++ b/packages/core/src/componentUtils.ts @@ -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) { diff --git a/packages/core/src/errorHandling.ts b/packages/core/src/errorHandling.ts index 7714bcf4..8a59d2ab 100644 --- a/packages/core/src/errorHandling.ts +++ b/packages/core/src/errorHandling.ts @@ -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 }