fix(runtime-core/async-component): fix error component when there are no error handlers

fix #2129
This commit is contained in:
Evan You
2020-09-16 11:10:16 -04:00
parent bad0ecb910
commit c7b4a379cf
3 changed files with 67 additions and 7 deletions

View File

@@ -117,7 +117,12 @@ export function defineAsyncComponent<
const onError = (err: Error) => {
pendingRequest = null
handleError(err, instance, ErrorCodes.ASYNC_COMPONENT_LOADER)
handleError(
err,
instance,
ErrorCodes.ASYNC_COMPONENT_LOADER,
!errorComponent /* do not throw in dev if user provided error component */
)
}
// suspense-controlled or SSR.
@@ -152,7 +157,7 @@ export function defineAsyncComponent<
if (timeout != null) {
setTimeout(() => {
if (!loaded.value) {
if (!loaded.value && !error.value) {
const err = new Error(
`Async component timed out after ${timeout}ms.`
)

View File

@@ -99,7 +99,8 @@ export function callWithAsyncErrorHandling(
export function handleError(
err: unknown,
instance: ComponentInternalInstance | null,
type: ErrorTypes
type: ErrorTypes,
throwInDev = true
) {
const contextVNode = instance ? instance.vnode : null
if (instance) {
@@ -131,10 +132,15 @@ export function handleError(
return
}
}
logError(err, type, contextVNode)
logError(err, type, contextVNode, throwInDev)
}
function logError(err: unknown, type: ErrorTypes, contextVNode: VNode | null) {
function logError(
err: unknown,
type: ErrorTypes,
contextVNode: VNode | null,
throwInDev = true
) {
if (__DEV__) {
const info = ErrorTypeStrings[type]
if (contextVNode) {
@@ -144,8 +150,12 @@ function logError(err: unknown, type: ErrorTypes, contextVNode: VNode | null) {
if (contextVNode) {
popWarningContext()
}
// crash in dev so it's more noticeable
throw err
// crash in dev by default so it's more noticeable
if (throwInDev) {
throw err
} else {
console.error(err)
}
} else {
// recover in prod to reduce the impact on end-user
console.error(err)