Skip to content

Commit

Permalink
Remove usages of "any" type
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkshiift committed Aug 27, 2024
1 parent 4840596 commit b02c41d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,9 +60,10 @@ export async function setConfig<K extends ConfigKey>(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);
}
}

Expand All @@ -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());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/screenshare/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
});
}
});
}

Expand Down
41 changes: 36 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ export function isSemverLower(version1: string, version2: string): boolean {
return false;
}

export async function tryWithFix(toDo: () => any, attemptFix: () => any, message: string) {
export async function tryWithFix<T>(toDo: () => T | Promise<T>, attemptFix: () => void | Promise<void>, message: string) {
try {
return await toDo();
} catch (error) {
console.error(chalk.bgRed("[Auto Fixer]"), message, error);
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)}`);
}
}
}
Expand All @@ -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<string, unknown>).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
}

0 comments on commit b02c41d

Please sign in to comment.