From 965683b9345316ed1771de1957fb494350905d85 Mon Sep 17 00:00:00 2001 From: Harry <45198715+harrytothemoon@users.noreply.github.com> Date: Thu, 8 Sep 2022 10:00:29 +0800 Subject: [PATCH] fix: handle the errorType which is overwrited --- packages/error-overlay/src/client.ts | 21 ++++++++++++--------- packages/error-overlay/src/iframeScript.tsx | 11 +++++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/error-overlay/src/client.ts b/packages/error-overlay/src/client.ts index 1e59ac6d4..23fad1657 100644 --- a/packages/error-overlay/src/client.ts +++ b/packages/error-overlay/src/client.ts @@ -17,7 +17,7 @@ let stackTraceLimit: number | undefined = undefined; let iframe: null | HTMLIFrameElement = null; let isLoadingIframe: boolean = false; let isIframeReady: boolean = false; -let errorType: errorTypeHandler.ErrorTypeEvent; +let errorTypeList: errorTypeHandler.ErrorTypeEvent[] = []; let hasBuildError: Boolean = false; let hasRuntimeError: Boolean = false; @@ -45,11 +45,11 @@ function onUnhandledError(ev: ErrorEvent) { } hasRuntimeError = true; - errorType = { + errorTypeList.push({ type: TYPE_UNHANDLED_ERROR, reason: error, frames: parseStack(error.stack) - }; + }); update(); } @@ -65,11 +65,11 @@ function onUnhandledRejection(ev: PromiseRejectionEvent) { } hasRuntimeError = true; - errorType = { + errorTypeList.push({ type: TYPE_UNHANDLED_REJECTION, reason: reason, frames: parseStack(reason.stack) - }; + }); update(); } @@ -114,18 +114,18 @@ function stopReportingRuntimeErrors() { function onBuildOk() { hasBuildError = false; - errorType = { type: TYPE_BUILD_OK }; + errorTypeList.push({ type: TYPE_BUILD_OK }); update(); } function onBuildError(message: string) { hasBuildError = true; - errorType = { type: TYPE_BUILD_ERROR, message }; + errorTypeList.push({ type: TYPE_BUILD_ERROR, message }); update(); } function onRefresh() { - errorType = { type: TYPE_REFRESH }; + errorTypeList.push({ type: TYPE_REFRESH }); } function applyStyles(element: HTMLElement, styles: Object) { @@ -178,11 +178,14 @@ function updateIframeContent() { //@ts-ignore const isRendered = iframe.contentWindow!.updateContent({ - errorType, + errorTypeList, hasBuildError, hasRuntimeError }); + //After the errors have been added to the queue of the error handler, we must clear the errorTypeList + errorTypeList = []; + if (!isRendered) { window.document.body.removeChild(iframe); iframe = null; diff --git a/packages/error-overlay/src/iframeScript.tsx b/packages/error-overlay/src/iframeScript.tsx index 3f9c1be92..e6a8e6540 100644 --- a/packages/error-overlay/src/iframeScript.tsx +++ b/packages/error-overlay/src/iframeScript.tsx @@ -13,8 +13,11 @@ let iframeRoot = null; let errorBody = null; let isFirstRender = true; -function render({ errorType, hasBuildError, hasRuntimeError }) { - errorTypeHandler.emit(errorType); +function render({ errorTypeList, hasBuildError, hasRuntimeError }) { + errorTypeList.forEach((errorType: errorTypeHandler.ErrorTypeEvent) => { + errorTypeHandler.emit(errorType); + }); + if (!hasBuildError && !hasRuntimeError) { return null; } @@ -22,12 +25,12 @@ function render({ errorType, hasBuildError, hasRuntimeError }) { } window.updateContent = function updateContent({ - errorType, + errorTypeList, hasBuildError, hasRuntimeError }) { let renderedElement = render({ - errorType, + errorTypeList, hasBuildError, hasRuntimeError });