🎨 uniformed error message (#1580)

This commit is contained in:
Troy Li 2021-07-19 15:45:08 +08:00 committed by GitHub
parent 438778d8d9
commit f00f2ca655
2 changed files with 16 additions and 16 deletions

5
src/error.ts Normal file
View File

@ -0,0 +1,5 @@
export class QiankunError extends Error {
constructor(message: string) {
super(`[qiankun]: ${message}`);
}
}

View File

@ -7,6 +7,7 @@ import { importEntry } from 'import-html-entry';
import { concat, forEach, mergeWith } from 'lodash'; import { concat, forEach, mergeWith } from 'lodash';
import type { LifeCycles, ParcelConfigObject } from 'single-spa'; import type { LifeCycles, ParcelConfigObject } from 'single-spa';
import getAddOns from './addons'; import getAddOns from './addons';
import { QiankunError } from './error';
import { getMicroAppStateActions } from './globalState'; import { getMicroAppStateActions } from './globalState';
import type { import type {
FrameworkConfiguration, FrameworkConfiguration,
@ -33,10 +34,10 @@ import {
function assertElementExist(element: Element | null | undefined, msg?: string) { function assertElementExist(element: Element | null | undefined, msg?: string) {
if (!element) { if (!element) {
if (msg) { 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 () => { return () => {
if (useLegacyRender) { if (useLegacyRender) {
if (strictStyleIsolation) throw new Error('[qiankun]: strictStyleIsolation can not be used with legacy render!'); if (strictStyleIsolation) throw new QiankunError('strictStyleIsolation can not be used with legacy render!');
if (scopedCSS) throw new Error('[qiankun]: experimentalStyleIsolation 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)); const appWrapper = document.getElementById(getWrapperId(appInstanceId));
assertElementExist( assertElementExist(appWrapper, `Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`);
appWrapper,
`[qiankun] Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`,
);
return appWrapper!; return appWrapper!;
} }
const element = elementGetter(); const element = elementGetter();
assertElementExist( assertElementExist(element, `Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`);
element,
`[qiankun] Wrapper element for ${appName} with instance ${appInstanceId} is not existed!`,
);
if (strictStyleIsolation && supportShadowDOM) { if (strictStyleIsolation && supportShadowDOM) {
return element!.shadowRoot!; return element!.shadowRoot!;
@ -178,13 +173,13 @@ function getRender(appName: string, appContent: string, legacyRender?: HTMLConte
switch (phase) { switch (phase) {
case 'loading': case 'loading':
case 'mounting': 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': case 'mounted':
return `[qiankun] Target container with ${container} not existed after ${appName} ${phase}!`; return `Target container with ${container} not existed after ${appName} ${phase}!`;
default: 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); assertElementExist(containerElement, errorMsg);
@ -239,7 +234,7 @@ function getLifecyclesFromExports(
return globalVariableExports; 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<void>; let prevAppUnmountedDeferred: Deferred<void>;