diff --git a/src/config.ts b/src/config.ts index 66bec2d..6ba18d3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,7 @@ import fs from "node:fs"; import path from "node:path"; import { dialog, ipcRenderer } from "electron"; -import { getGoofCordFolderPath, tryWithFix } from "./utils"; +import { getErrorMessage, getGoofCordFolderPath, tryWithFix } from "./utils"; import type { Config, ConfigKey } from "./configTypes"; export let cachedConfig: Config; @@ -60,9 +60,10 @@ export async function setConfig(entry: K, value: Config[K]) cachedConfig[entry] = value; const toSave = JSON.stringify(cachedConfig, undefined, 2); void fs.promises.writeFile(getConfigLocation(), toSave, "utf-8"); - } catch (e: any) { - console.error("setConfig function errored:", e); - dialog.showErrorBox("GoofCord was unable to save the settings", e.toString()); + } catch (e: unknown) { + const errorMessage = getErrorMessage(e); + console.error("setConfig function errored:", errorMessage); + dialog.showErrorBox("GoofCord was unable to save the settings", errorMessage); } } @@ -74,9 +75,10 @@ export async function setConfigBulk(toSet: Config) { cachedConfig = toSet; const toSave = JSON.stringify(toSet, undefined, 2); await fs.promises.writeFile(getConfigLocation(), toSave, "utf-8"); - } catch (e: any) { - console.error("setConfigBulk function errored:", e); - dialog.showErrorBox("GoofCord was unable to save the settings", e.toString()); + } catch (e: unknown) { + const errorMessage = getErrorMessage(e); + console.error("setConfigBulk function errored:", errorMessage); + dialog.showErrorBox("GoofCord was unable to save the settings", errorMessage.toString()); } } diff --git a/src/screenshare/preload.ts b/src/screenshare/preload.ts index 3f45695..0434aaf 100755 --- a/src/screenshare/preload.ts +++ b/src/screenshare/preload.ts @@ -101,9 +101,9 @@ async function addDisplays() { document.body.appendChild(selectionElem); // Attach event listeners after elements are added to the DOM - document.querySelectorAll(".desktop-capturer-selection__btn").forEach((button) => { + for (const button of document.querySelectorAll(".desktop-capturer-selection__btn")) { button.addEventListener("click", () => selectSource(button.getAttribute("data-id"), button.getAttribute("title"))); - }); + } }); } diff --git a/src/utils.ts b/src/utils.ts index 54da6b0..253dace 100755 --- a/src/utils.ts +++ b/src/utils.ts @@ -67,7 +67,7 @@ export function isSemverLower(version1: string, version2: string): boolean { return false; } -export async function tryWithFix(toDo: () => any, attemptFix: () => any, message: string) { +export async function tryWithFix(toDo: () => T | Promise, attemptFix: () => void | Promise, message: string) { try { return await toDo(); } catch (error) { @@ -75,9 +75,9 @@ export async function tryWithFix(toDo: () => any, attemptFix: () => any, message await attemptFix(); try { return await toDo(); - } catch (error: any) { + } catch (error) { console.error(chalk.bgRedBright("[Auto Fixer FAIL]"), message, error); - dialog.showErrorBox("Auto fixer tried to fix an issue, but failed", `${message}\n\n${error.toString()}`); + dialog.showErrorBox("Auto fixer tried to fix an issue, but failed", `${message}\n\n${getErrorMessage(error)}`); } } } @@ -94,9 +94,40 @@ export async function readOrCreateFolder(path: string) { export async function tryCreateFolder(path: string) { try { await fs.mkdir(path, { recursive: true }); - } catch (e: any) { - if (e.code !== "EEXIST") { + } catch (e: unknown) { + if (e instanceof Error && 'code' in e && e.code !== "EEXIST") { console.error(e); } } } + +type ErrorWithMessage = { + message: string +} + +function isErrorWithMessage(error: unknown): error is ErrorWithMessage { + return ( + typeof error === 'object' && + error !== null && + 'message' in error && + typeof (error as Record).message === 'string' + ) +} + +function toErrorWithMessage(maybeError: unknown): ErrorWithMessage { + if (isErrorWithMessage(maybeError)) { + return maybeError + } + + try { + return new Error(JSON.stringify(maybeError)) + } catch { + // fallback in case there's an error stringifying the maybeError + // with circular references for example. + return new Error(String(maybeError)) + } +} + +export function getErrorMessage(error: unknown): string { + return toErrorWithMessage(error).message +} \ No newline at end of file