From 67ea7b3ce83f8d2b88d89ba06ae42582cd7957d8 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:30:16 +0800 Subject: [PATCH] Fix: [ALAS] Error interception in the main thread --- webapp/packages/main/src/coreService.ts | 4 +++ webapp/packages/main/src/createMainWindow.ts | 4 +-- .../main/src/serviceLogic/createAlas.ts | 32 ++++++++++++------- .../main/src/serviceLogic/createInstaller.ts | 21 +++++++++--- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/webapp/packages/main/src/coreService.ts b/webapp/packages/main/src/coreService.ts index 645a7a19a..c9fb70d02 100644 --- a/webapp/packages/main/src/coreService.ts +++ b/webapp/packages/main/src/coreService.ts @@ -65,6 +65,10 @@ export class CoreService { return this.currentService; } + onError(e: Error | any) { + logger.error(`currentServiceIndex:${this.stepIndex}` + (e as unknown as any).toString()); + } + reset() { this.stepIndex = 0; } diff --git a/webapp/packages/main/src/createMainWindow.ts b/webapp/packages/main/src/createMainWindow.ts index b1f65ddce..a438c5643 100644 --- a/webapp/packages/main/src/createMainWindow.ts +++ b/webapp/packages/main/src/createMainWindow.ts @@ -25,7 +25,6 @@ export const createMainWindow = async () => { browserWindow.webContents.on('preload-error', (event, preloadPath, error) => { logger.error('------------preload-error------------'); - logger.error(`event:${JSON.stringify(event)}`); logger.error(`preloadPath:${preloadPath}`); logger.error(`error:${error}`); }); @@ -37,8 +36,7 @@ export const createMainWindow = async () => { } if (level === 3) { logger.info('------------console-message------------'); - logger.error(`event:${JSON.stringify(event)}`); - logger.error(`console-message:${message} line:${line} sourceId:${sourceId}`); + logger.error(`console-message:${message} \n line:${line} \n sourceId:${sourceId}`); return; } }); diff --git a/webapp/packages/main/src/serviceLogic/createAlas.ts b/webapp/packages/main/src/serviceLogic/createAlas.ts index ecaa3f1b3..a864acb6a 100644 --- a/webapp/packages/main/src/serviceLogic/createAlas.ts +++ b/webapp/packages/main/src/serviceLogic/createAlas.ts @@ -4,26 +4,32 @@ import type {CallbackFun} from '/@/coreService'; import logger from '/@/logger'; export const createAlas: CallbackFun = async ctx => { - const alas = new PyShell(webuiPath, webuiArgs); - alas.on('error', function (err: string) { - if(!err) return; + let alas: PyShell | null = null; + try { + alas = new PyShell(webuiPath, webuiArgs); + } catch (e) { + ctx.onError(e); + } + + alas?.on('error', function (err: string) { + if (!err) return; logger.error('alas.error:' + err); ctx.sendLaunchLog(err); }); - alas.end(function (err: string) { - if(!err) return; + alas?.end(function (err: string) { + if (!err) return; logger.info('alas.end:' + err); ctx.sendLaunchLog(err); throw err; }); - alas.on('stdout', function (message) { + alas?.on('stdout', function (message) { ctx.sendLaunchLog(message); }); - alas.on('message', function (message) { + alas?.on('message', function (message) { ctx.sendLaunchLog(message); }); - alas.on('stderr', function (message: string) { + alas?.on('stderr', function (message: string) { ctx.sendLaunchLog(message); /** * Receive logs, judge if Alas is ready @@ -33,10 +39,14 @@ export const createAlas: CallbackFun = async ctx => { * `[Errno 10048] error while attempting to bind on address ('0.0.0.0', 22267): ` */ if (message.includes('Application startup complete') || message.includes('bind on address')) { - alas.removeAllListeners('stderr'); - alas.removeAllListeners('message'); - alas.removeAllListeners('stdout'); + alas?.removeAllListeners('stderr'); + alas?.removeAllListeners('message'); + alas?.removeAllListeners('stdout'); } }); + + alas?.on('pythonError', err => { + ctx.onError('alas pythonError:' + err); + }); return alas; }; diff --git a/webapp/packages/main/src/serviceLogic/createInstaller.ts b/webapp/packages/main/src/serviceLogic/createInstaller.ts index f43913a00..845242a51 100644 --- a/webapp/packages/main/src/serviceLogic/createInstaller.ts +++ b/webapp/packages/main/src/serviceLogic/createInstaller.ts @@ -7,17 +7,23 @@ export const createInstaller: CallbackFun = async (ctx, next) => { if (process.argv.includes(ALAS_RELAUNCH_ARGV)) { return next(); } - const installer = new PyShell(installerPath, installerArgs); - installer.on('error', function (err: string) { - if(!err) return; + let installer: PyShell | null = null; + try { + installer = new PyShell(installerPath, installerArgs); + } catch (err) { + ctx.onError(err); + } + + installer?.on('error', function (err: string) { + if (!err) return; logger.error('installer.error:' + err); ctx.sendLaunchLog(err); }); installer?.end(function (err: string) { - if(!err) return; + if (!err) return; logger.info('installer.end:' + err); ctx.sendLaunchLog(err); - throw err; + // throw err; }); installer?.on('stdout', function (message) { ctx.sendLaunchLog(message); @@ -28,5 +34,10 @@ export const createInstaller: CallbackFun = async (ctx, next) => { installer?.on('stderr', function (message: string) { ctx.sendLaunchLog(message); }); + + installer?.on('pythonError', err => { + ctx.onError('alas pythonError :' + err); + }); + return installer; };