From 18d13a23ac30c5617facd56aff646383a4676931 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Tue, 24 May 2022 14:58:11 +0300 Subject: [PATCH] Relocate some explicit error handlings to proper level of abstraction Co-authored-by: Mikko Aspiala Signed-off-by: Janne Savolainen --- .../check-for-updates-tray-item.injectable.ts | 20 ++++++++++++---- ...application-update-tray-item.injectable.ts | 16 ++++++++++--- .../about-app-tray-item.injectable.ts | 20 ++++++++++++---- .../open-app-tray-item.injectable.ts | 18 ++++++++++++--- .../open-preferences-tray-item.injectable.ts | 16 ++++++++++--- .../quit-app-tray-item.injectable.ts | 16 ++++++++++--- .../tray-menu-item-registrator.injectable.ts | 23 ++++++++++++++----- src/main/tray/tray.ts | 9 +------- 8 files changed, 104 insertions(+), 34 deletions(-) diff --git a/src/main/application-update/check-for-updates-tray-item.injectable.ts b/src/main/application-update/check-for-updates-tray-item.injectable.ts index 8ae1d2c544..3354d908c9 100644 --- a/src/main/application-update/check-for-updates-tray-item.injectable.ts +++ b/src/main/application-update/check-for-updates-tray-item.injectable.ts @@ -13,6 +13,9 @@ import updatesAreBeingDiscoveredInjectable from "../../common/application-update import progressOfUpdateDownloadInjectable from "../../common/application-update/progress-of-update-download/progress-of-update-download.injectable"; import assert from "assert"; import processCheckingForUpdatesInjectable from "./check-for-updates/process-checking-for-updates.injectable"; +import { withErrorSuppression } from "../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; +import withErrorLoggingInjectable from "../../common/utils/with-error-logging/with-error-logging.injectable"; const checkForUpdatesTrayItemInjectable = getInjectable({ id: "check-for-updates-tray-item", @@ -25,6 +28,7 @@ const checkForUpdatesTrayItemInjectable = getInjectable({ const downloadingUpdateState = di.inject(updateIsBeingDownloadedInjectable); const checkingForUpdatesState = di.inject(updatesAreBeingDiscoveredInjectable); const processCheckingForUpdates = di.inject(processCheckingForUpdatesInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "check-for-updates", @@ -51,11 +55,19 @@ const checkForUpdatesTrayItemInjectable = getInjectable({ visible: computed(() => updatingIsEnabled), - click: async () => { - await processCheckingForUpdates(); + click: pipeline( + async () => { + await processCheckingForUpdates(); - await showApplicationWindow(); - }, + await showApplicationWindow(); + }, + + withErrorLoggingFor(() => "[TRAY]: Checking for updates failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/application-update/install-application-update-tray-item.injectable.ts b/src/main/application-update/install-application-update-tray-item.injectable.ts index 6b1cb9fec7..2f938964f8 100644 --- a/src/main/application-update/install-application-update-tray-item.injectable.ts +++ b/src/main/application-update/install-application-update-tray-item.injectable.ts @@ -8,6 +8,9 @@ import { trayMenuItemInjectionToken } from "../tray/tray-menu-item/tray-menu-ite import quitAndInstallUpdateInjectable from "../electron-app/features/quit-and-install-update.injectable"; import discoveredUpdateVersionInjectable from "../../common/application-update/discovered-update-version/discovered-update-version.injectable"; import updateIsBeingDownloadedInjectable from "../../common/application-update/update-is-being-downloaded/update-is-being-downloaded.injectable"; +import { withErrorSuppression } from "../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; +import withErrorLoggingInjectable from "../../common/utils/with-error-logging/with-error-logging.injectable"; const installApplicationUpdateTrayItemInjectable = getInjectable({ id: "install-update-tray-item", @@ -16,6 +19,7 @@ const installApplicationUpdateTrayItemInjectable = getInjectable({ const quitAndInstallUpdate = di.inject(quitAndInstallUpdateInjectable); const discoveredVersionState = di.inject(discoveredUpdateVersionInjectable); const downloadingUpdateState = di.inject(updateIsBeingDownloadedInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "install-update", @@ -34,9 +38,15 @@ const installApplicationUpdateTrayItemInjectable = getInjectable({ () => !!discoveredVersionState.value.get() && !downloadingUpdateState.value.get(), ), - click: () => { - quitAndInstallUpdate(); - }, + click: pipeline( + quitAndInstallUpdate, + + withErrorLoggingFor(() => "[TRAY]: Update installation failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/tray/tray-menu-item/implementations/about-app-tray-item.injectable.ts b/src/main/tray/tray-menu-item/implementations/about-app-tray-item.injectable.ts index 5e7090c6d4..5fb1a9f34f 100644 --- a/src/main/tray/tray-menu-item/implementations/about-app-tray-item.injectable.ts +++ b/src/main/tray/tray-menu-item/implementations/about-app-tray-item.injectable.ts @@ -8,6 +8,9 @@ import showApplicationWindowInjectable from "../../../start-main-application/len import showAboutInjectable from "../../../menu/show-about.injectable"; import { trayMenuItemInjectionToken } from "../tray-menu-item-injection-token"; import { computed } from "mobx"; +import withErrorLoggingInjectable from "../../../../common/utils/with-error-logging/with-error-logging.injectable"; +import { withErrorSuppression } from "../../../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; const aboutAppTrayItemInjectable = getInjectable({ id: "about-app-tray-item", @@ -16,6 +19,7 @@ const aboutAppTrayItemInjectable = getInjectable({ const productName = di.inject(productNameInjectable); const showApplicationWindow = di.inject(showApplicationWindowInjectable); const showAbout = di.inject(showAboutInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "about-app", @@ -25,11 +29,19 @@ const aboutAppTrayItemInjectable = getInjectable({ enabled: computed(() => true), visible: computed(() => true), - click: async () => { - await showApplicationWindow(); + click: pipeline( + async () => { + await showApplicationWindow(); - await showAbout(); - }, + await showAbout(); + }, + + withErrorLoggingFor(() => "[TRAY]: Opening of show about failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/tray/tray-menu-item/implementations/open-app-tray-item.injectable.ts b/src/main/tray/tray-menu-item/implementations/open-app-tray-item.injectable.ts index 5780c45777..ff19d7718a 100644 --- a/src/main/tray/tray-menu-item/implementations/open-app-tray-item.injectable.ts +++ b/src/main/tray/tray-menu-item/implementations/open-app-tray-item.injectable.ts @@ -7,6 +7,9 @@ import { trayMenuItemInjectionToken } from "../tray-menu-item-injection-token"; import productNameInjectable from "../../../app-paths/app-name/product-name.injectable"; import showApplicationWindowInjectable from "../../../start-main-application/lens-window/show-application-window.injectable"; import { computed } from "mobx"; +import withErrorLoggingInjectable from "../../../../common/utils/with-error-logging/with-error-logging.injectable"; +import { withErrorSuppression } from "../../../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; const openAppTrayItemInjectable = getInjectable({ id: "open-app-tray-item", @@ -14,6 +17,7 @@ const openAppTrayItemInjectable = getInjectable({ instantiate: (di) => { const productName = di.inject(productNameInjectable); const showApplicationWindow = di.inject(showApplicationWindowInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "open-app", @@ -23,9 +27,17 @@ const openAppTrayItemInjectable = getInjectable({ enabled: computed(() => true), visible: computed(() => true), - click: async () => { - await showApplicationWindow(); - }, + click: pipeline( + async () => { + await showApplicationWindow(); + }, + + withErrorLoggingFor(() => "[TRAY]: Opening of application window failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/tray/tray-menu-item/implementations/open-preferences-tray-item.injectable.ts b/src/main/tray/tray-menu-item/implementations/open-preferences-tray-item.injectable.ts index 73bb524896..8c062f6a29 100644 --- a/src/main/tray/tray-menu-item/implementations/open-preferences-tray-item.injectable.ts +++ b/src/main/tray/tray-menu-item/implementations/open-preferences-tray-item.injectable.ts @@ -6,12 +6,16 @@ import { getInjectable } from "@ogre-tools/injectable"; import { trayMenuItemInjectionToken } from "../tray-menu-item-injection-token"; import navigateToPreferencesInjectable from "../../../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable"; import { computed } from "mobx"; +import { withErrorSuppression } from "../../../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; +import withErrorLoggingInjectable from "../../../../common/utils/with-error-logging/with-error-logging.injectable"; const openPreferencesTrayItemInjectable = getInjectable({ id: "open-preferences-tray-item", instantiate: (di) => { const navigateToPreferences = di.inject(navigateToPreferencesInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "open-preferences", @@ -21,9 +25,15 @@ const openPreferencesTrayItemInjectable = getInjectable({ enabled: computed(() => true), visible: computed(() => true), - click: () => { - navigateToPreferences(); - }, + click: pipeline( + navigateToPreferences, + + withErrorLoggingFor(() => "[TRAY]: Opening of preferences failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/tray/tray-menu-item/implementations/quit-app-tray-item.injectable.ts b/src/main/tray/tray-menu-item/implementations/quit-app-tray-item.injectable.ts index 78410c7784..894a823511 100644 --- a/src/main/tray/tray-menu-item/implementations/quit-app-tray-item.injectable.ts +++ b/src/main/tray/tray-menu-item/implementations/quit-app-tray-item.injectable.ts @@ -6,12 +6,16 @@ import { getInjectable } from "@ogre-tools/injectable"; import { trayMenuItemInjectionToken } from "../tray-menu-item-injection-token"; import { computed } from "mobx"; import stopServicesAndExitAppInjectable from "../../../stop-services-and-exit-app.injectable"; +import { withErrorSuppression } from "../../../../common/utils/with-error-suppression/with-error-suppression"; +import { pipeline } from "@ogre-tools/fp"; +import withErrorLoggingInjectable from "../../../../common/utils/with-error-logging/with-error-logging.injectable"; const quitAppTrayItemInjectable = getInjectable({ id: "quit-app-tray-item", instantiate: (di) => { const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable); + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); return { id: "quit-app", @@ -21,9 +25,15 @@ const quitAppTrayItemInjectable = getInjectable({ enabled: computed(() => true), visible: computed(() => true), - click: () => { - stopServicesAndExitApp(); - }, + click: pipeline( + stopServicesAndExitApp, + + withErrorLoggingFor(() => "[TRAY]: Quitting application failed."), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), }; }, diff --git a/src/main/tray/tray-menu-item/tray-menu-item-registrator.injectable.ts b/src/main/tray/tray-menu-item/tray-menu-item-registrator.injectable.ts index c6b0b5b270..6cbd9e5d33 100644 --- a/src/main/tray/tray-menu-item/tray-menu-item-registrator.injectable.ts +++ b/src/main/tray/tray-menu-item/tray-menu-item-registrator.injectable.ts @@ -12,17 +12,21 @@ import type { LensMainExtension } from "../../../extensions/lens-main-extension" import type { TrayMenuItem } from "./tray-menu-item-injection-token"; import { trayMenuItemInjectionToken } from "./tray-menu-item-injection-token"; import type { TrayMenuRegistration } from "../tray-menu-registration"; +import { withErrorSuppression } from "../../../common/utils/with-error-suppression/with-error-suppression"; +import type { WithErrorLoggingFor } from "../../../common/utils/with-error-logging/with-error-logging.injectable"; +import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable"; const trayMenuItemRegistratorInjectable = getInjectable({ id: "tray-menu-item-registrator", instantiate: (di) => (extension, installationCounter) => { const mainExtension = extension as LensMainExtension; + const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); pipeline( mainExtension.trayMenus, - flatMap(toItemInjectablesFor(mainExtension, installationCounter)), + flatMap(toItemInjectablesFor(mainExtension, installationCounter, withErrorLoggingFor)), (injectables) => di.register(...injectables), ); @@ -33,8 +37,7 @@ const trayMenuItemRegistratorInjectable = getInjectable({ export default trayMenuItemRegistratorInjectable; - -const toItemInjectablesFor = (extension: LensMainExtension, installationCounter: number) => { +const toItemInjectablesFor = (extension: LensMainExtension, installationCounter: number, withErrorLoggingFor: WithErrorLoggingFor) => { const _toItemInjectables = (parentId: string | null) => (registration: TrayMenuRegistration): Injectable[] => { const trayItemId = registration.id || kebabCase(registration.label || ""); const id = `${trayItemId}-tray-menu-item-for-extension-${extension.sanitizedExtensionId}-instance-${installationCounter}`; @@ -52,9 +55,17 @@ const toItemInjectablesFor = (extension: LensMainExtension, installationCounter: label: computed(() => registration.label || ""), tooltip: registration.toolTip, - click: () => { - registration.click?.(registration); - }, + click: pipeline( + () => { + registration.click?.(registration); + }, + + withErrorLoggingFor(() => `[TRAY]: Clicking of tray item "${trayItemId}" from extension "${extension.sanitizedExtensionId}" failed.`), + + // TODO: Find out how to improve typing so that instead of + // x => withErrorSuppression(x) there could only be withErrorSuppression + (x) => withErrorSuppression(x), + ), enabled: computed(() => !!registration.enabled), visible: computed(() => true), diff --git a/src/main/tray/tray.ts b/src/main/tray/tray.ts index 561f8adebd..4d7e39c344 100644 --- a/src/main/tray/tray.ts +++ b/src/main/tray/tray.ts @@ -82,14 +82,7 @@ const toTrayMenuOptions = (trayMenuItems: TrayMenuItem[]) => { submenu: _toTrayMenuOptions(trayMenuItem.id), click: () => { - try { - trayMenuItem.click?.(); - } catch (error) { - logger.error( - `${TRAY_LOG_PREFIX}: clicking item "${trayMenuItem.id} failed."`, - { error }, - ); - } + trayMenuItem.click?.(); }, } : {