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>
render: RenderFunction<P>
renderError?: (e: Error) => any
data?(): Partial<D>
beforeCreate?(): void
created?(): void

View 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) {

View 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
}