feat: renderError

This commit is contained in:
Evan You 2018-09-23 20:30:26 -04:00
parent 08c8fdb62e
commit 44d1a8efcb
3 changed files with 25 additions and 10 deletions

View File

@ -34,6 +34,7 @@ export interface MountedComponent<D = Data, P = Data> extends Component {
$options: ComponentOptions<D, P> $options: ComponentOptions<D, P>
render: RenderFunction<P> render: RenderFunction<P>
renderError?: (e: Error) => any
data?(): Partial<D> data?(): Partial<D>
beforeCreate?(): void beforeCreate?(): void
created?(): void created?(): void

View File

@ -14,6 +14,7 @@ import {
import { initializeWatch, teardownWatch } from './componentWatch' import { initializeWatch, teardownWatch } from './componentWatch'
import { Data, ComponentOptions } from './componentOptions' import { Data, ComponentOptions } from './componentOptions'
import { createRenderProxy } from './componentProxy' import { createRenderProxy } from './componentProxy'
import { handleError, ErrorTypes } from './errorHandling'
export function createComponentInstance( export function createComponentInstance(
vnode: VNode, vnode: VNode,
@ -53,11 +54,24 @@ export function createComponentInstance(
} }
export function renderInstanceRoot(instance: MountedComponent) { export function renderInstanceRoot(instance: MountedComponent) {
// TODO handle render error let vnode
return normalizeComponentRoot( try {
instance.render.call(instance.$proxy, instance.$props, instance.$slots), vnode = instance.render.call(
instance.$parentVNode 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) { export function teardownComponentInstance(instance: MountedComponent) {

View File

@ -2,9 +2,10 @@ import { MountedComponent } from './component'
export const enum ErrorTypes { export const enum ErrorTypes {
LIFECYCLE = 1, LIFECYCLE = 1,
RENDER = 2, RENDER,
NATIVE_EVENT_HANDLER = 3, RENDER_ERROR,
COMPONENT_EVENT_HANDLER = 4 NATIVE_EVENT_HANDLER,
COMPONENT_EVENT_HANDLER
} }
const globalHandlers: Function[] = [] const globalHandlers: Function[] = []
@ -19,8 +20,7 @@ export function globalHandleError(handler: () => void) {
export function handleError( export function handleError(
err: Error, err: Error,
instance: MountedComponent, instance: MountedComponent,
type: ErrorTypes, type: ErrorTypes
code: number
) { ) {
// TODO // TODO
} }