From 4d62faad02827d543dd8da7b605d3e3c972e36e0 Mon Sep 17 00:00:00 2001 From: David Michon Date: Tue, 16 Jul 2024 15:55:34 -0700 Subject: [PATCH] [heft] Handle errors when IPC messaging host (#4832) Co-authored-by: David Michon --- .../main_2024-07-15-21-27.json | 10 ++++++++++ libraries/operation-graph/src/WatchLoop.ts | 20 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 common/changes/@rushstack/operation-graph/main_2024-07-15-21-27.json diff --git a/common/changes/@rushstack/operation-graph/main_2024-07-15-21-27.json b/common/changes/@rushstack/operation-graph/main_2024-07-15-21-27.json new file mode 100644 index 00000000000..789587ea170 --- /dev/null +++ b/common/changes/@rushstack/operation-graph/main_2024-07-15-21-27.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/operation-graph", + "comment": "Handle errors when sending IPC messages to host.", + "type": "patch" + } + ], + "packageName": "@rushstack/operation-graph" +} \ No newline at end of file diff --git a/libraries/operation-graph/src/WatchLoop.ts b/libraries/operation-graph/src/WatchLoop.ts index 99b9e31537c..ca911c32bf7 100644 --- a/libraries/operation-graph/src/WatchLoop.ts +++ b/libraries/operation-graph/src/WatchLoop.ts @@ -133,6 +133,20 @@ export class WatchLoop implements IWatchLoopState { let runRequestedFromHost: boolean = true; let status: OperationStatus = OperationStatus.Ready; + function tryMessageHost( + message: ISyncEventMessage | IRequestRunEventMessage | IAfterExecuteEventMessage + ): void { + if (!host.send) { + return reject(new Error('Host does not support IPC')); + } + + try { + host.send(message); + } catch (err) { + reject(new Error(`Unable to communicate with host: ${err}`)); + } + } + function requestRunFromHost(requestor?: string): void { if (runRequestedFromHost) { return; @@ -145,7 +159,7 @@ export class WatchLoop implements IWatchLoopState { requestor }; - host.send!(requestRunMessage); + tryMessageHost(requestRunMessage); } function sendSync(): void { @@ -153,7 +167,7 @@ export class WatchLoop implements IWatchLoopState { event: 'sync', status }; - host.send!(syncMessage); + tryMessageHost(syncMessage); } host.on('message', async (message: CommandMessageFromHost) => { @@ -189,7 +203,7 @@ export class WatchLoop implements IWatchLoopState { event: 'after-execute', status }; - host.send!(afterExecuteMessage); + tryMessageHost(afterExecuteMessage); } return; }