From 8a5ff8ce6b8b1329b1f2686f634607de8831d453 Mon Sep 17 00:00:00 2001 From: wuhailong Date: Tue, 4 Jun 2024 21:52:36 +0800 Subject: [PATCH] update error message Signed-off-by: wuhailong Change-Id: I3012bff09b5b5ca88710377547e60adaee327a4e --- compiler/src/ark_utils.ts | 24 +++++----- .../ark_compiler/bundle/bundle_mode.ts | 10 ++-- .../ark_compiler/module/module_mode.ts | 29 +++++------- .../ark_compiler/module/module_source_file.ts | 2 +- compiler/src/process_ui_syntax.ts | 2 +- .../common/check_shared_module.test.ts | 2 +- .../module/module_mode.test.ts | 37 +++++++++++++-- .../module/ohmUrl/ohmUrl.test.ts | 47 +++++++++++++++---- .../utils/processProjectConfig.ts | 3 +- 9 files changed, 107 insertions(+), 49 deletions(-) diff --git a/compiler/src/ark_utils.ts b/compiler/src/ark_utils.ts index 8b43b88c6..ba34eb6f1 100644 --- a/compiler/src/ark_utils.ts +++ b/compiler/src/ark_utils.ts @@ -94,10 +94,10 @@ export function getNormalizedOhmUrlByFilepath(filePath: string, projectConfig: O // ---> @normalized:N&&&/entry/ets/xxx/yyy& let pkgInfo = projectConfig.pkgContextInfo[pkgName]; if (pkgInfo === undefined) { - logger.error(red, - `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${filePath} belongs to is correctly configured ` + - `and the corresponding file name is correct(including case-sensitivity)`, reset); + logger.error(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset); } let recordName = `${pkgInfo.bundleName}&${pkgName}${projectFilePath}&${pkgInfo.version}`; if (isRecordName) { @@ -186,10 +186,10 @@ function processPackageDir(params: Object): string { } } - logger.error(red, - `ArkTS:ERROR Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${originalFilePath} belongs to is correctly configured ` + - `and the corresponding file name is correct(including case-sensitivity)`, reset); + logger.error(red,'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${originalFilePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset); return originalFilePath; } @@ -219,10 +219,10 @@ function processPackageDir(params: Object): string { } } - logger.error(red, - `ArkTS:ERROR Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${originalFilePath} belongs to is correctly configured ` + - `and the corresponding file name is correct(including case-sensitivity)`, reset); + logger.error(red,'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${originalFilePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset); return originalFilePath; } diff --git a/compiler/src/fast_build/ark_compiler/bundle/bundle_mode.ts b/compiler/src/fast_build/ark_compiler/bundle/bundle_mode.ts index 374ac23e1..4f41e23a5 100644 --- a/compiler/src/fast_build/ark_compiler/bundle/bundle_mode.ts +++ b/compiler/src/fast_build/ark_compiler/bundle/bundle_mode.ts @@ -95,7 +95,7 @@ export class BundleMode extends CommonMode { } else if (rollupBundleFileSet[fileName].type === 'chunk') { rollupBundleSourceCode = rollupBundleFileSet[fileName].code; } else { - this.throwArkTsCompilerError(`ArkTS:INTERNAL ERROR: Failed to retrieve source code ` + + this.throwArkTsCompilerError('ArkTS:INTERNAL ERROR: Failed to retrieve source code ' + `for ${fileName} from rollup file set.`); } fs.writeFileSync(cacheOutputPath, rollupBundleSourceCode, 'utf-8'); @@ -129,7 +129,7 @@ export class BundleMode extends CommonMode { if (!fs.existsSync(cacheFilePath)) { this.throwArkTsCompilerError( `ArkTS:INTERNAL ERROR: Failed to get bundle cached abc from ${cacheFilePath} in incremental build.` + - `Please try to rebuild the project.`); + 'Please try to rebuild the project.'); } if (fs.existsSync(cacheAbcFilePath)) { const hashCacheFileContentData: string = toHashData(cacheFilePath); @@ -156,7 +156,7 @@ export class BundleMode extends CommonMode { const splittedBundles: any[] = this.getSplittedBundles(); this.invokeTs2AbcWorkersToGenAbc(splittedBundles); } else { - this.throwArkTsCompilerError(`ArkTS:INTERNAL ERROR: Invalid compilation mode.`); + this.throwArkTsCompilerError('ArkTS:INTERNAL ERROR: Invalid compilation mode.'); } } @@ -206,9 +206,9 @@ export class BundleMode extends CommonMode { const child = this.triggerAsync(() => { return childProcess.exec(genAbcCmd, { windowsHide: true }); }); - child.on('close', (code: any) => { + child.on('close', (code: number) => { if (code === FAIL) { - this.throwArkTsCompilerError(`ArkTS:ERROR Failed to execute es2abc.\n` + + this.throwArkTsCompilerError('ArkTS:ERROR Failed to execute es2abc.\n' + `genAbcCmd: ${genAbcCmd}`); } this.afterCompilationProcess(); diff --git a/compiler/src/fast_build/ark_compiler/module/module_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_mode.ts index d0995326b..ab379d436 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_mode.ts @@ -32,6 +32,7 @@ import { EXTNAME_TS, EXTNAME_TXT, FAIL, + SUCCESS, FILESINFO, FILESINFO_TXT, MAX_WORKER_NUMBER, @@ -237,11 +238,11 @@ export class ModuleMode extends CommonMode { continue; } const moduleInfo: Object = module.getModuleInfo(moduleId); - if (moduleInfo['meta']['isNodeEntryFile'] && !this.useNormalizedOHMUrl) { - this.getPackageEntryInfo(moduleId, moduleInfo['meta'], pkgEntryInfos); + if (moduleInfo.meta.isNodeEntryFile && !this.useNormalizedOHMUrl) { + this.getPackageEntryInfo(moduleId, moduleInfo.meta, pkgEntryInfos); } - this.processModuleInfos(moduleId, moduleInfos, moduleInfo['meta']); + this.processModuleInfos(moduleId, moduleInfos, moduleInfo.meta); } if (!this.useNormalizedOHMUrl) { this.getDynamicImportEntryInfo(pkgEntryInfos); @@ -251,7 +252,7 @@ export class ModuleMode extends CommonMode { this.pkgEntryInfos = pkgEntryInfos; } - private isUsingNormalizedOHMUrl() { + private isUsingNormalizedOHMUrl(): boolean { return !!this.projectConfig.useNormalizedOHMUrl; } @@ -544,9 +545,10 @@ export class ModuleMode extends CommonMode { return childProcess.exec(genAbcCmd, { windowsHide: true }); }); child.on('close', (code: any) => { - if (code === FAIL) { - this.throwArkTsCompilerError(`ArkTS:ERROR Failed to execute es2abc\n` + - `genAbcCmd: ${genAbcCmd}`); + if (code !== SUCCESS) { + this.throwArkTsCompilerError('ArkTS:ERROR Failed to execute es2abc.\n' + + `Error Message: ${errMsg}\n` + + `The execution command of es2abc is: ${genAbcCmd}`); } stopEvent(eventGenAbc, true); this.triggerEndSignal(); @@ -562,13 +564,8 @@ export class ModuleMode extends CommonMode { errMsg += data.toString(); }); - child.stderr.on('end', (data: any) => { - if (errMsg !== undefined && errMsg.length > 0) { - this.logger.error(red, errMsg, reset); - } - }); } catch (e) { - this.throwArkTsCompilerError('ArkTS:ERROR Failed to execute es2abc. Error message: ' + e.toString()); + this.throwArkTsCompilerError(`ArkTS:ERROR Failed to execute es2abc.\nError message: ${e.toString()}\n`); } } @@ -594,7 +591,7 @@ export class ModuleMode extends CommonMode { if (!fs.existsSync(cacheFilePath)) { this.throwArkTsCompilerError( `ArkTS:INTERNAL ERROR: Failed to get module cache abc from ${cacheFilePath} in incremental build.` + - `Please try to rebuild the project.`); + 'Please try to rebuild the project.'); } if (fs.existsSync(cacheProtoFilePath)) { const hashCacheFileContentData: string = toHashData(cacheFilePath); @@ -658,7 +655,7 @@ export class ModuleMode extends CommonMode { const worker: Object = cluster.fork(workerData); worker.on('message', (errorMsg) => { this.logger.error(red, errorMsg.data.toString(), reset); - this.throwArkTsCompilerError(`ArkTS:ERROR Failed to execute ts2abc.`); + this.throwArkTsCompilerError('ArkTS:ERROR Failed to execute ts2abc.'); }); }); } @@ -791,7 +788,7 @@ export class ModuleMode extends CommonMode { try { childProcess.execSync(cmd, { windowsHide: true }); } catch (e) { - this.throwArkTsCompilerError(`ArkTS:ERROR failed to generate npm proto file to abc. Error message: ` + e.toString()); + this.throwArkTsCompilerError('ArkTS:ERROR failed to generate npm proto file to abc. Error message: ' + e.toString()); } } diff --git a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts index 153122689..f2d786d4d 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts @@ -259,7 +259,7 @@ export class ModuleSourceFile { } static updataCachedTransformedMockConfigInfo(newMockConfig: Object, cachedTransMockConfigInfo: Object, - transMockConfigCachePath: string, transMockConfigPath: string) { + transMockConfigCachePath: string, transMockConfigPath: string): void { for (const key in newMockConfig) { if (!Object.prototype.hasOwnProperty.call(cachedTransMockConfigInfo, key)) { cachedTransMockConfigInfo[key] = newMockConfig[key]; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 0d89c7f96..7dd688501 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -365,7 +365,7 @@ export function processUISyntax(program: ts.Program, ut = false, if (projectConfig.compileHar && !projectConfig.useTsHar) { let warnMessage: string = 'If you use @Sendable in js har, an exception will occur during runtime.\n' + 'Please use @Sendable in ts har. You can configure {"name": "UseTsHar", ' + - '"value": "true"} in the "metadata" in the module.json5 file in har.' + '"value": "true"} in the "metadata" in the module.json5 file in har.'; transformLog.errors.push({ type: LogType.WARN, message: warnMessage, diff --git a/compiler/test/ark_compiler_ut/common/check_shared_module.test.ts b/compiler/test/ark_compiler_ut/common/check_shared_module.test.ts index 9f02d05ee..b396f0133 100644 --- a/compiler/test/ark_compiler_ut/common/check_shared_module.test.ts +++ b/compiler/test/ark_compiler_ut/common/check_shared_module.test.ts @@ -22,7 +22,7 @@ import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; import { collectSharedModule } from '../../../lib/fast_build/ark_compiler/check_shared_module'; import { LogInfo } from '../../../lib/utils'; -mocha.describe('test check_import_module file api', function() { +mocha.describe('test check_shared_module file api', function() { mocha.before(function() { this.rollup = new RollUpPluginMock(); }); diff --git a/compiler/test/ark_compiler_ut/module/module_mode.test.ts b/compiler/test/ark_compiler_ut/module/module_mode.test.ts index 457315b6f..d53064fd7 100644 --- a/compiler/test/ark_compiler_ut/module/module_mode.test.ts +++ b/compiler/test/ark_compiler_ut/module/module_mode.test.ts @@ -1511,12 +1511,43 @@ mocha.describe('test module_mode file api', function () { let parentEvent = undefined; moduleMode.generateMergedAbcOfEs2AbcMock(parentEvent); await sleep(1000); - expect(stub.calledWithMatch('ArkTS:ERROR Failed to execute es2abc\n')).to.be.true; + expect(stub.calledWithMatch('ArkTS:ERROR Failed to execute es2abc.\nError Message: ')).to.be.true; triggerAsyncStub.restore(); stub.restore(); }); - mocha.it('13-2: test the error message of generateMergedAbcOfEs2Abc handle error', function () { + mocha.it('13-2: test the error message of generateMergedAbcOfEs2Abc throw error(Failed to startup es2abc)', async function () { + this.rollup.build(); + const moduleMode = new ModuleModeMock(this.rollup); + const child = { + on: sinon.stub(), + stderr: { + on: sinon.stub(), + }, + }; + const triggerAsyncStub = sinon.stub(moduleMode, 'triggerAsync').callsFake((callback) => { + callback(); + return child; + }); + const stub = sinon.stub(moduleMode, 'throwArkTsCompilerError'); + let errorEventCallback; + child.on.callsFake((event, callback) => { + if (event === 'error') { + errorEventCallback = callback; + } + }); + let parentEvent = undefined; + moduleMode.generateMergedAbcOfEs2AbcMock(parentEvent); + if (errorEventCallback) { + errorEventCallback(new Error('test error')); + } + await sleep(100); + expect(stub.calledWith('Error: test error')).to.be.true; + triggerAsyncStub.restore(); + stub.restore(); + }); + + mocha.it('13-3: test the error message of generateMergedAbcOfEs2Abc handle error', function () { this.rollup.build(); const moduleMode = new ModuleModeMock(this.rollup); const triggerAsyncStub = sinon.stub(moduleMode, 'triggerAsync').throws(new Error('Execution failed')); @@ -1526,7 +1557,7 @@ mocha.describe('test module_mode file api', function () { moduleMode.generateMergedAbcOfEs2AbcMock(parentEvent); } catch (e) { } - expect(stub.calledWithMatch('ArkTS:ERROR Failed to execute es2abc. Error message: ')).to.be.true; + expect(stub.calledWith('ArkTS:ERROR Failed to execute es2abc.\nError message: Error: Execution failed\n')).to.be.true; triggerAsyncStub.restore(); stub.restore(); }); diff --git a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts index 06eba9c13..1f39dc8ce 100644 --- a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts +++ b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts @@ -20,7 +20,8 @@ import sinon from 'sinon'; import { getOhmUrlByFilepath, getOhmUrlByHspName, - getOhmUrlBySystemApiOrLibRequest + getOhmUrlBySystemApiOrLibRequest, + getNormalizedOhmUrlByFilepath } from '../../../../lib/ark_utils'; import { PACKAGES } from '../../../../lib/pre_define'; import projectConfig from '../../utils/processProjectConfig'; @@ -172,10 +173,10 @@ mocha.describe('generate ohmUrl', function () { const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) const loggerStub = sinon.stub(logger, 'error'); getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); - expect(loggerStub.calledWith(red, - `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${filePath} belongs to is correctly configured ` + - `and the corresponding file name is correct(including case-sensitivity)`, reset)).to.be.true; + expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; loggerStub.restore(); }); @@ -191,10 +192,10 @@ mocha.describe('generate ohmUrl', function () { const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) const loggerStub = sinon.stub(logger, 'error'); getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); - expect(loggerStub.calledWith(red, - `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${filePath} belongs to is correctly configured ` + - `and the corresponding file name is correct(including case-sensitivity)`, reset)).to.be.true; + expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; loggerStub.restore(); }); @@ -871,6 +872,34 @@ mocha.describe('generate ohmUrl', function () { expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; }); + mocha.it('the error message of getNormalizedOhmUrlByFilepath', function () { + this.rollup.build(); + const pkgParams = { + pkgName: 'json5', + pkgPath: `${projectConfig.projectRootPath}/entry/oh_modules/json5`, + isRecordName: false + }; + projectConfig.pkgContextInfo = { + 'json5': undefined + }; + const red: string = '\u001b[31m'; + const reset: string = '\u001b[39m'; + const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; + const moduleName: string = 'entry'; + const importerFile: string = 'importTest.ts'; + const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) + const loggerStub = sinon.stub(logger, 'error'); + try { + getNormalizedOhmUrlByFilepath(filePath, projectConfig, logger, pkgParams, importerFile); + } catch (e) { + } + expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + + `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + + `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + + '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; + loggerStub.restore(); + }); + mocha.it('transform mockConfigInfo', function () { this.rollup.preview(); ModuleSourceFile.mockConfigInfo = PRVIEW_MOCK_CONFIG; diff --git a/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts b/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts index b99260d74..dc769e88e 100644 --- a/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts +++ b/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts @@ -17,7 +17,7 @@ import path from 'path'; process.env.compileTool = 'rollup'; -const projectConfig: any = {}; +const projectConfig: Object = {}; projectConfig.aceModuleJsonPath = path.resolve(__dirname, '../../../test/ark_compiler_ut/config/module.json'); projectConfig.modulePathMap = { @@ -30,5 +30,6 @@ projectConfig.harNameOhmMap = { "@ohos/sharedLibrary": "@bundle:UtTestApplication/sharedLibrary/ets/index" }; projectConfig.cachePath = '../../test/ark_compiler_ut/cache/'; +projectConfig.pkgContextInfo = {}; export default projectConfig; \ No newline at end of file -- Gitee