From 592c1579f301705417632a6e5e68c49fda342a7e Mon Sep 17 00:00:00 2001 From: Manoah Tervoort <149895ja@gmail.com> Date: Fri, 6 Dec 2024 15:12:31 +0100 Subject: [PATCH] refactor: move utils --- src-electron/electron-main.ts | 2 +- src-electron/main/downloads.ts | 3 +-- src-electron/main/fs.ts | 2 +- src-electron/main/ipc.ts | 2 +- src-electron/main/log.ts | 26 ------------------- src-electron/main/screen.ts | 2 +- src-electron/main/security.ts | 2 +- src-electron/main/session.ts | 2 +- src-electron/main/shortcuts.ts | 2 +- src-electron/main/updater.ts | 2 +- src-electron/{ => main}/utils.ts | 29 +++++++++++++++++++--- src-electron/main/window/window-base.ts | 2 +- src-electron/main/window/window-main.ts | 4 +-- src-electron/main/window/window-media.ts | 2 +- src-electron/main/window/window-state.ts | 2 +- src-electron/main/window/window-website.ts | 2 +- 16 files changed, 41 insertions(+), 45 deletions(-) delete mode 100644 src-electron/main/log.ts rename src-electron/{ => main}/utils.ts (86%) diff --git a/src-electron/electron-main.ts b/src-electron/electron-main.ts index 12d63460e7..6a0dfc1a82 100644 --- a/src-electron/electron-main.ts +++ b/src-electron/electron-main.ts @@ -16,10 +16,10 @@ import { join } from 'upath'; import { PLATFORM } from './constants'; import { cancelAllDownloads } from './main/downloads'; -import { captureElectronError } from './main/log'; import { initScreenListeners } from './main/screen'; import { initSessionListeners } from './main/session'; import { initUpdater } from './main/updater'; +import { captureElectronError } from './main/utils'; import { closeOtherWindows, sendToWindow } from './main/window/window-base'; import { authorizedClose, diff --git a/src-electron/main/downloads.ts b/src-electron/main/downloads.ts index fd44ef1f3a..1d779cd0cd 100644 --- a/src-electron/main/downloads.ts +++ b/src-electron/main/downloads.ts @@ -3,8 +3,7 @@ import { ElectronDownloadManager } from 'electron-dl-manager'; import { ensureDir } from 'fs-extra'; import { basename } from 'upath'; -import { fetchJson } from './../utils'; -import { captureElectronError } from './log'; +import { captureElectronError, fetchJson } from './utils'; import { sendToWindow } from './window/window-base'; import { mainWindow } from './window/window-main'; diff --git a/src-electron/main/fs.ts b/src-electron/main/fs.ts index 74e09539e2..e0fa0230ee 100644 --- a/src-electron/main/fs.ts +++ b/src-electron/main/fs.ts @@ -10,7 +10,7 @@ import { } from 'src/constants/media'; import { basename, dirname, join, toUnix } from 'upath'; -import { captureElectronError } from './log'; +import { captureElectronError } from './utils'; import { sendToWindow } from './window/window-base'; import { mainWindow } from './window/window-main'; diff --git a/src-electron/main/ipc.ts b/src-electron/main/ipc.ts index 4323f23624..ddac826749 100644 --- a/src-electron/main/ipc.ts +++ b/src-electron/main/ipc.ts @@ -20,7 +20,6 @@ import { } from 'electron'; import { PLATFORM } from '../constants'; -import { getAppVersion, isSelf } from './../utils'; import { downloadFile } from './downloads'; import { openFileDialog, @@ -36,6 +35,7 @@ import { unregisterAllShortcuts, unregisterShortcut, } from './shortcuts'; +import { getAppVersion, isSelf } from './utils'; import { logToWindow } from './window/window-base'; import { mainWindow, toggleAuthorizedClose } from './window/window-main'; import { mediaWindow, moveMediaWindow } from './window/window-media'; diff --git a/src-electron/main/log.ts b/src-electron/main/log.ts deleted file mode 100644 index 5bcdbd0256..0000000000 --- a/src-electron/main/log.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent'; - -import { captureException } from '@sentry/electron/main'; - -import { IS_DEV } from '../constants'; - -/** - * Logs an error to the console or to Sentry - * @param error The error to log - * @param context The context to log with the error - */ -export function captureElectronError( - error: Error | string | unknown, - context?: ExclusiveEventHintOrCaptureContext, -) { - if (error instanceof Error && error.cause) { - captureElectronError(error.cause, context); - } - - if (IS_DEV) { - console.error(error); - console.warn('context', context); - } else { - captureException(error, context); - } -} diff --git a/src-electron/main/screen.ts b/src-electron/main/screen.ts index fd81d4163f..2be055145f 100644 --- a/src-electron/main/screen.ts +++ b/src-electron/main/screen.ts @@ -2,7 +2,7 @@ import type { Display, ScreenPreferences } from 'src/types'; import { app, type BrowserWindow, screen } from 'electron'; -import { captureElectronError } from './log'; +import { captureElectronError } from './utils'; import { mainWindow } from './window/window-main'; import { mediaWindow, moveMediaWindow } from './window/window-media'; diff --git a/src-electron/main/security.ts b/src-electron/main/security.ts index 69463e6952..c2e9b94a0a 100644 --- a/src-electron/main/security.ts +++ b/src-electron/main/security.ts @@ -1,6 +1,6 @@ import { app, session, shell } from 'electron'; -import { isSelf, isTrustedDomain } from './../utils'; +import { isSelf, isTrustedDomain } from './utils'; import { logToWindow } from './window/window-base'; import { mainWindow } from './window/window-main'; diff --git a/src-electron/main/session.ts b/src-electron/main/session.ts index 4cef309270..f5a27c0dea 100644 --- a/src-electron/main/session.ts +++ b/src-electron/main/session.ts @@ -10,7 +10,7 @@ import { isSelf, isTrustedDomain, isValidUrl, -} from './../utils'; +} from './utils'; export let urlVariables: undefined | UrlVariables; diff --git a/src-electron/main/shortcuts.ts b/src-electron/main/shortcuts.ts index 182ba13dd1..e67d6a28b3 100644 --- a/src-electron/main/shortcuts.ts +++ b/src-electron/main/shortcuts.ts @@ -2,7 +2,7 @@ import type { SettingsValues } from 'src/types'; import { globalShortcut } from 'electron'; -import { captureElectronError } from './log'; +import { captureElectronError } from './utils'; import { sendToWindow } from './window/window-base'; import { mainWindow } from './window/window-main'; diff --git a/src-electron/main/updater.ts b/src-electron/main/updater.ts index b326ddf3cd..a1eb336dcc 100644 --- a/src-electron/main/updater.ts +++ b/src-electron/main/updater.ts @@ -4,7 +4,7 @@ import { exists } from 'fs-extra'; import { join } from 'path'; import { PLATFORM } from './../constants'; -import { captureElectronError } from './log'; +import { captureElectronError } from './utils'; export async function initUpdater() { autoUpdater.on('error', (error, message) => { diff --git a/src-electron/utils.ts b/src-electron/main/utils.ts similarity index 86% rename from src-electron/utils.ts rename to src-electron/main/utils.ts index a4c8dbe149..48f5372293 100644 --- a/src-electron/utils.ts +++ b/src-electron/main/utils.ts @@ -1,9 +1,11 @@ +import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent'; + +import { captureException } from '@sentry/electron/main'; import { version } from 'app/package.json'; import { app } from 'electron'; -import { IS_DEV, JW_DOMAINS, TRUSTED_DOMAINS } from './constants'; -import { captureElectronError } from './main/log'; -import { urlVariables } from './main/session'; +import { IS_DEV, JW_DOMAINS, TRUSTED_DOMAINS } from './../constants'; +import { urlVariables } from './session'; /** * Gets the current app version @@ -154,6 +156,27 @@ export const fetchJson = async ( return null; }; +/** + * Logs an error to the console or to Sentry + * @param error The error to log + * @param context The context to log with the error + */ +export function captureElectronError( + error: Error | string | unknown, + context?: ExclusiveEventHintOrCaptureContext, +) { + if (error instanceof Error && error.cause) { + captureElectronError(error.cause, context); + } + + if (IS_DEV) { + console.error(error); + console.warn('context', context); + } else { + captureException(error, context); + } +} + /** * Throttles a function to only run once every `delay` milliseconds * @param func The function to throttle diff --git a/src-electron/main/window/window-base.ts b/src-electron/main/window/window-base.ts index f1785707cd..8768e39491 100644 --- a/src-electron/main/window/window-base.ts +++ b/src-electron/main/window/window-base.ts @@ -8,8 +8,8 @@ import { } from 'electron'; import { join, resolve } from 'path'; -import { captureElectronError } from './../log'; import { urlVariables } from './../session'; +import { captureElectronError } from './../utils'; import { StatefulBrowserWindow } from './window-state'; export function closeOtherWindows(source: BrowserWindow) { diff --git a/src-electron/main/window/window-main.ts b/src-electron/main/window/window-main.ts index 92867c559e..31dea8f32a 100644 --- a/src-electron/main/window/window-main.ts +++ b/src-electron/main/window/window-main.ts @@ -1,9 +1,9 @@ import type { BrowserWindow } from 'electron'; import { PLATFORM } from 'app/src-electron/constants'; -import { throttle } from 'app/src-electron/utils'; -import { cancelAllDownloads } from '../downloads'; +import { cancelAllDownloads } from './../downloads'; +import { throttle } from './../utils'; import { closeOtherWindows, createWindow, sendToWindow } from './window-base'; import { createMediaWindow, moveMediaWindow } from './window-media'; diff --git a/src-electron/main/window/window-media.ts b/src-electron/main/window/window-media.ts index 7fc7e80ff6..8115e8c63b 100644 --- a/src-electron/main/window/window-media.ts +++ b/src-electron/main/window/window-media.ts @@ -4,8 +4,8 @@ import type { ScreenPreferences } from 'src/types'; import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants'; import { join, resolve } from 'path'; -import { captureElectronError } from './../log'; import { getAllScreens, getWindowScreen, screenPreferences } from './../screen'; +import { captureElectronError } from './../utils'; import { createWindow, sendToWindow } from './window-base'; import { mainWindow } from './window-main'; diff --git a/src-electron/main/window/window-state.ts b/src-electron/main/window/window-state.ts index 5ff734b8d7..3a080951eb 100644 --- a/src-electron/main/window/window-state.ts +++ b/src-electron/main/window/window-state.ts @@ -8,7 +8,7 @@ import { import { ensureDirSync, readJsonSync, writeJsonSync } from 'fs-extra'; import { dirname, join } from 'path'; -import { captureElectronError } from './../log'; +import { captureElectronError } from './../utils'; interface ExtraOptions { /** The name of file. Defaults to `window-state.json`. */ diff --git a/src-electron/main/window/window-website.ts b/src-electron/main/window/window-website.ts index ab9dcff57a..915408008d 100644 --- a/src-electron/main/window/window-website.ts +++ b/src-electron/main/window/window-website.ts @@ -3,7 +3,7 @@ import type { NavigateWebsiteAction } from 'src/types'; import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants'; import { type BrowserWindow, systemPreferences, type Video } from 'electron'; -import { captureElectronError } from './../log'; +import { captureElectronError } from './../utils'; import { createWindow, logToWindow, sendToWindow } from './window-base'; import { mainWindow } from './window-main';