diff --git a/src/helpers/config.ts b/src/helpers/config.ts index 1411998..3a15f82 100644 --- a/src/helpers/config.ts +++ b/src/helpers/config.ts @@ -13,9 +13,6 @@ async function readConfig(): Promise { crashHandler("Error reading config file.", err); return "CRASH"; }); - if (args.verbose) { - console.log(`Read config file: ${args.config}\n${conf}`); - } return conf; } /** @@ -29,9 +26,9 @@ async function readConfig(): Promise { * // Prints "https://mastodon.social" */ export default async function getConfig(): Promise { - let cfg: config; + let conf: config; try { - cfg = JSON.parse( + conf = JSON.parse( stripJsonComments( await readConfig().catch((err) => { crashHandler("Error reading config file.", err); @@ -39,9 +36,12 @@ export default async function getConfig(): Promise { }) ) ); - } catch (err) { - crashHandler("Error parsing config file.", err); + } catch (err: unknown) { + crashHandler("Error parsing config file.", Error(err as string)); return {} as config; } - return cfg; + if (args.verbose) { + console.log(`Read config file: ${args.config}\n${JSON.stringify(conf)}`); + } + return conf; } diff --git a/src/helpers/error.ts b/src/helpers/error.ts index 32d852e..1ab534c 100644 --- a/src/helpers/error.ts +++ b/src/helpers/error.ts @@ -8,10 +8,10 @@ import args from "./cli.js"; * only displays when `args.verbose` is true * @returns This function will never return. */ -export default function crashHandler(msg: string, e: Error | unknown) { - console.error(msg); +export default function crashHandler(msg: string, e: Error) { + console.error(`${msg}: ${e.name}`); if (args.verbose) { - console.error(`--BEGIN FULL ERROR--\n${e}\n--END FULL ERROR--`); + console.error(`--BEGIN FULL ERROR--\n${e.message}\n--END FULL ERROR--`); } else console.error("Run with -v to see the full error."); exit(1); diff --git a/src/remote.ts b/src/remote.ts index 07ca602..0812b6e 100644 --- a/src/remote.ts +++ b/src/remote.ts @@ -1,6 +1,7 @@ import { search } from "booru"; import Post from "booru/dist/structures/Post"; // Ce n'est pas bien -import { createReadStream, createWriteStream, unlink } from "fs"; +import { createReadStream, createWriteStream } from "fs"; +import { unlink } from "fs/promises"; import got from "got-cjs"; import stream from "node:stream"; import { promisify } from "node:util"; @@ -26,7 +27,7 @@ export default async function getRemoteImage(conf: config) { return [] as Post[]; }); if (searchResults.length === 0) { - crashHandler("No posts found.", ""); + crashHandler("Error searching for posts.", Error("No posts found.")); return; } const post = searchResults[0]; @@ -57,11 +58,8 @@ export default async function getRemoteImage(conf: config) { await postImage(str, sensitivity, conf); // Delete the image that it downloaded - unlink(filename, (err: unknown) => { - if (err) { - crashHandler("Error deleting downloaded image.", err); - return; - } + await unlink(filename).catch((err: Error) => { + crashHandler("Error deleting downloaded image.", err); }); if (args.verbose) console.log(`Successfully deleted image ${filename}`); exit(0);