diff --git a/src/error.ts b/src/error.ts new file mode 100644 index 0000000..a05d3a6 --- /dev/null +++ b/src/error.ts @@ -0,0 +1,5 @@ +export class QiankunError extends Error { + constructor(message: string) { + super(`[qiankun]: ${message}`); + } +} diff --git a/src/loader.ts b/src/loader.ts index bb46f69..37ac667 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -7,6 +7,7 @@ import { importEntry } from 'import-html-entry'; import { concat, forEach, mergeWith } from 'lodash'; import type { LifeCycles, ParcelConfigObject } from 'single-spa'; import getAddOns from './addons'; +import { QiankunError } from './error'; import { getMicroAppStateActions } from './globalState'; import type { FrameworkConfiguration, @@ -33,10 +34,10 @@ import { function assertElementExist(element: Element | null | undefined, msg?: string) { if (!element) { if (msg) { - throw new Error(msg); + throw new QiankunError(msg); } - throw new Error('[qiankun] element not existed!'); + throw new QiankunError('element not existed!'); } } @@ -118,22 +119,16 @@ function getAppWrapperGetter( ) { return () => { if (useLegacyRender) { - if (strictStyleIsolation) throw new Error('[qiankun]: strictStyleIsolation can not be used with legacy render!'); - if (scopedCSS) throw new Error('[qiankun]: experimentalStyleIsolation can not be used with legacy render!'); + if (strictStyleIsolation) throw new QiankunError('strictStyleIsolation can not be used with legacy render!'); + if (scopedCSS) throw new QiankunError('experimentalStyleIsolation can not be used with legacy render!'); const appWrapper = document.getElementById(getWrapperId(appInstanceId)); - assertElementExist( - appWrapper, - `[qiankun] Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`, - ); + assertElementExist(appWrapper, `Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`); return appWrapper!; } const element = elementGetter(); - assertElementExist( - element, - `[qiankun] Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`, - ); + assertElementExist(element, `Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`); if (strictStyleIsolation && supportShadowDOM) { return element!.shadowRoot!; @@ -178,13 +173,13 @@ function getRender(appName: string, appContent: string, legacyRender?: HTMLConte switch (phase) { case 'loading': case 'mounting': - return `[qiankun] Target container with ${container} not existed while ${appName} ${phase}!`; + return `Target container with ${container} not existed while ${appName} ${phase}!`; case 'mounted': - return `[qiankun] Target container with ${container} not existed after ${appName} ${phase}!`; + return `Target container with ${container} not existed after ${appName} ${phase}!`; default: - return `[qiankun] Target container with ${container} not existed while ${appName} rendering!`; + return `Target container with ${container} not existed while ${appName} rendering!`; } })(); assertElementExist(containerElement, errorMsg); @@ -239,7 +234,7 @@ function getLifecyclesFromExports( return globalVariableExports; } - throw new Error(`[qiankun] You need to export lifecycle functions in ${appName} entry`); + throw new QiankunError(`You need to export lifecycle functions in ${appName} entry`); } let prevAppUnmountedDeferred: Deferred;