Skip to content

Commit

Permalink
fix(runtime-core):vuejs#5657
Browse files Browse the repository at this point in the history
  • Loading branch information
javastation committed Apr 6, 2022
1 parent 245230e commit 83b463b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
25 changes: 24 additions & 1 deletion packages/runtime-core/__tests__/vnode.spec.ts
Expand Up @@ -12,7 +12,7 @@ import {
} from '../src/vnode'
import { Data } from '../src/component'
import { ShapeFlags, PatchFlags } from '@vue/shared'
import { h, reactive, isReactive, setBlockTracking, ref } from '../src'
import { h, reactive, isReactive, setBlockTracking, ref, withCtx } from '../src'
import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
import { setCurrentRenderingInstance } from '../src/componentRenderContext'

Expand Down Expand Up @@ -614,6 +614,29 @@ describe('vnode', () => {
]))
expect(vnode.dynamicChildren).toStrictEqual([])
})
// #5657
test('error of slot function execution should not affect block tracking', () => {
const error = new Error('slot execution error')
let caughtError
const slot = withCtx(
() => {
throw error
},
{ type: {}, appContext: {} } as any
)
try {
slot()
} catch (e) {
caughtError = e
}
expect(caughtError).toBe(error)
expect(
`[Vue warn]: Unhandled error during execution of slot function`
).toHaveBeenWarned()

const vnode = (openBlock(), createBlock('div'))
expect(vnode.dynamicChildren).toStrictEqual([])
})
})

describe('transformVNodeArgs', () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/runtime-core/src/componentRenderContext.ts
@@ -1,6 +1,7 @@
import { ComponentInternalInstance } from './component'
import { devtoolsComponentUpdated } from './devtools'
import { setBlockTracking } from './vnode'
import { handleError, ErrorCodes } from './errorHandling'

/**
* mark the current rendering instance for asset resolution (e.g.
Expand Down Expand Up @@ -89,10 +90,16 @@ export function withCtx(
setBlockTracking(-1)
}
const prevInstance = setCurrentRenderingInstance(ctx)
const res = fn(...args)
setCurrentRenderingInstance(prevInstance)
if (renderFnWithContext._d) {
setBlockTracking(1)
let res
try {
res = fn(...args)
} catch (e) {
handleError(e, prevInstance, ErrorCodes.SLOT_FUNCTION)
} finally {
setCurrentRenderingInstance(prevInstance)
if (renderFnWithContext._d) {
setBlockTracking(1)
}
}

if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime-core/src/errorHandling.ts
Expand Up @@ -20,7 +20,8 @@ export const enum ErrorCodes {
APP_WARN_HANDLER,
FUNCTION_REF,
ASYNC_COMPONENT_LOADER,
SCHEDULER
SCHEDULER,
SLOT_FUNCTION
}

export const ErrorTypeStrings: Record<number | string, string> = {
Expand Down Expand Up @@ -54,7 +55,8 @@ export const ErrorTypeStrings: Record<number | string, string> = {
[ErrorCodes.ASYNC_COMPONENT_LOADER]: 'async component loader',
[ErrorCodes.SCHEDULER]:
'scheduler flush. This is likely a Vue internals bug. ' +
'Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/core'
'Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/core',
[ErrorCodes.SLOT_FUNCTION]: 'slot function'
}

export type ErrorTypes = LifecycleHooks | ErrorCodes
Expand Down

0 comments on commit 83b463b

Please sign in to comment.