1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Relocate some explicit error handlings to proper level of abstraction

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-24 14:58:11 +03:00
parent e1733d382a
commit 18d13a23ac
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
8 changed files with 104 additions and 34 deletions

View File

@ -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),
),
};
},

View File

@ -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),
),
};
},

View File

@ -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),
),
};
},

View File

@ -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),
),
};
},

View File

@ -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),
),
};
},

View File

@ -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),
),
};
},

View File

@ -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<TrayMenuItem, TrayMenuItem, void>[] => {
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),

View File

@ -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?.();
},
}
: {