From 632365a3b9f58b995f9d3f1721d683f403a93bef Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 24 Nov 2022 13:24:32 -0500 Subject: [PATCH] Add error notification on shell sync failure Signed-off-by: Sebastian Malton --- .../message-to-channel-injection-token.ts | 6 ++++++ .../shell-sync/common/failure-channel.ts | 10 ++++++++++ .../main/emit-failure.injectable.ts | 19 +++++++++++++++++++ .../shell-sync/main/setup-shell.injectable.ts | 7 ++++++- .../renderer/failure-listener.injectable.ts | 19 +++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/features/shell-sync/common/failure-channel.ts create mode 100644 src/features/shell-sync/main/emit-failure.injectable.ts create mode 100644 src/features/shell-sync/renderer/failure-listener.injectable.ts diff --git a/src/common/utils/channel/message-to-channel-injection-token.ts b/src/common/utils/channel/message-to-channel-injection-token.ts index d179ab9870..3ffd75f4f7 100644 --- a/src/common/utils/channel/message-to-channel-injection-token.ts +++ b/src/common/utils/channel/message-to-channel-injection-token.ts @@ -10,6 +10,12 @@ export interface SendMessageToChannel { (channel: MessageChannel, message: Message): void; } +export type MessageChannelSender = Channel extends MessageChannel + ? () => void + : Channel extends MessageChannel + ? (message: Message) => void + : never; + export const sendMessageToChannelInjectionToken = getInjectionToken({ id: "send-message-to-message-channel", }); diff --git a/src/features/shell-sync/common/failure-channel.ts b/src/features/shell-sync/common/failure-channel.ts new file mode 100644 index 0000000000..aed25b0da0 --- /dev/null +++ b/src/features/shell-sync/common/failure-channel.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import type { MessageChannel } from "../../../common/utils/channel/message-channel-listener-injection-token"; + +export const shellSyncFailedChannel: MessageChannel = { + id: "shell-sync-failed-channel", +}; diff --git a/src/features/shell-sync/main/emit-failure.injectable.ts b/src/features/shell-sync/main/emit-failure.injectable.ts new file mode 100644 index 0000000000..5abdfafa52 --- /dev/null +++ b/src/features/shell-sync/main/emit-failure.injectable.ts @@ -0,0 +1,19 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import type { MessageChannelSender } from "../../../common/utils/channel/message-to-channel-injection-token"; +import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token"; +import { shellSyncFailedChannel } from "../common/failure-channel"; + +const emitShellSyncFailedInjectable = getInjectable({ + id: "emit-shell-sync-failed", + instantiate: (di): MessageChannelSender => { + const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); + + return (error) => sendMessageToChannel(shellSyncFailedChannel, error); + }, +}); + +export default emitShellSyncFailedInjectable; diff --git a/src/features/shell-sync/main/setup-shell.injectable.ts b/src/features/shell-sync/main/setup-shell.injectable.ts index 0c91f049f4..a534411a1f 100644 --- a/src/features/shell-sync/main/setup-shell.injectable.ts +++ b/src/features/shell-sync/main/setup-shell.injectable.ts @@ -10,6 +10,7 @@ import isSnapPackageInjectable from "../../../common/vars/is-snap-package.inject import electronAppInjectable from "../../../main/electron-app/electron-app.injectable"; import computeShellEnvironmentInjectable from "./compute-shell-environment.injectable"; import userShellSettingInjectable from "../../../common/user-store/shell-setting.injectable"; +import emitShellSyncFailedInjectable from "./emit-failure.injectable"; const setupShellInjectable = getInjectable({ id: "setup-shell", @@ -20,6 +21,7 @@ const setupShellInjectable = getInjectable({ const electronApp = di.inject(electronAppInjectable); const resolvedUserShellSetting = di.inject(userShellSettingInjectable); const computeShellEnvironment = di.inject(computeShellEnvironmentInjectable); + const emitShellSyncFailed = di.inject(emitShellSyncFailedInjectable); return { id: "setup-shell", @@ -29,7 +31,10 @@ const setupShellInjectable = getInjectable({ const result = await computeShellEnvironment(resolvedUserShellSetting.get()); if (!result.callWasSuccessful) { - return void logger.error(`[SHELL-SYNC]: ${result.error}`); + logger.error(`[SHELL-SYNC]: ${result.error}`); + emitShellSyncFailed(result.error); + + return; } const env = result.response; diff --git a/src/features/shell-sync/renderer/failure-listener.injectable.ts b/src/features/shell-sync/renderer/failure-listener.injectable.ts new file mode 100644 index 0000000000..2014397fad --- /dev/null +++ b/src/features/shell-sync/renderer/failure-listener.injectable.ts @@ -0,0 +1,19 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getMessageChannelListenerInjectable } from "../../../common/utils/channel/message-channel-listener-injection-token"; +import showErrorNotificationInjectable from "../../../renderer/components/notifications/show-error-notification.injectable"; +import { shellSyncFailedChannel } from "../common/failure-channel"; + +const shellSyncFailureListenerInjectable = getMessageChannelListenerInjectable({ + id: "notification", + channel: shellSyncFailedChannel, + handler: (di) => { + const showErrorNotification = di.inject(showErrorNotificationInjectable); + + return (errorMessage) => showErrorNotification(`Failed to sync shell environment: ${errorMessage}`); + }, +}); + +export default shellSyncFailureListenerInjectable;