From 874ecb77290befa3877daca153a1d7e19ee3a2d5 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Sat, 8 Jan 2022 14:47:45 -0600 Subject: [PATCH] Add more error handling Signed-off-by: Sam Therapy --- src/local.ts | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/local.ts b/src/local.ts index 699e20a..d26fda8 100644 --- a/src/local.ts +++ b/src/local.ts @@ -72,10 +72,39 @@ if (args.verbose) { } // JSON object read from config file -const data = JSON.parse(fs.readFileSync("./config.json", "utf8")); +let data: { + instance: string, + type: "misskey" | "mastodon" | "pleroma", + accessToken: string, + refreshToken: string | null +}; -const sfw_files = fs.readdirSync(args.directory + "/sfw"); -const nsfw_files = fs.readdirSync(args.directory + "/nsfw"); +try { + data = JSON.parse(fs.readFileSync(args.config, "utf8")); + if (args.verbose) { + console.log(`Config: ${JSON.stringify(data)}`); + } +} +catch (e: unknown) { + console.error(`Error reading config file: ${e}`); + exit(1); +} +let sfw_files: string[] = []; +try { + sfw_files = fs.readdirSync(args.directory + "/sfw"); +} +catch (e: unknown) { + console.error(`Error reading SFW image directory: ${e}`); + exit(1); +} +let nsfw_files: string[] = []; +try { + nsfw_files = fs.readdirSync(args.directory + "/nsfw"); +} +catch (e: unknown) { + console.error(`Error reading NSFW image directory: ${e}`); + exit(1); +} const random = Math.floor(Math.random() * (sfw_files.length + nsfw_files.length)); // Get image from directory and mark it as sensitive if it's in the nsfw directory