From 1ea174b629b728644263212c0e52d8de32a19f08 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Sat, 8 Jan 2022 11:12:43 -0600 Subject: [PATCH] Make error messages more clear Signed-off-by: Sam Therapy --- package.json | 1 - src/gen-token.ts | 3 ++ src/local.ts | 83 +++++++++++++++++++++++++++++------------------- yarn.lock | 24 +++++++------- 4 files changed, 66 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index a29c395..0da2fb3 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "lint": "eslint --ext .ts src", "local": "node ./dist/local.js", "test": "echo \"No tests yet!\" && exit 0" - }, "repository": "https://git.freecumextremist.com/NotSam/fediverse-imagebot.git", "author": "Sam Therapy ", diff --git a/src/gen-token.ts b/src/gen-token.ts index 0937ea4..025c378 100644 --- a/src/gen-token.ts +++ b/src/gen-token.ts @@ -79,6 +79,7 @@ callDetector(instance).then(type => { }) .catch((err: Error) => { // catch for fetchAccessToken console.error("Access Token fetch failed."); + console.error("Run with -v to see the full error."); if (args.verbose) console.error(err); exit(1); @@ -86,6 +87,7 @@ callDetector(instance).then(type => { }) .catch((err: Error) => { // catch for registerApp console.error("App registration failure!"); + console.error("Run with -v to see the full error."); if (args.verbose) console.error(err); exit(1); @@ -96,6 +98,7 @@ async function callDetector(instance: string) { const type = await detector(instance).catch( (err) => { console.error("This does not seem to be a valid instance!"); console.error("Supported instance types: Mastodon, Misskey, Pleroma"); + console.error("Run with -v to see the full error."); if (args.verbose) console.error(err); exit(1); diff --git a/src/local.ts b/src/local.ts index ef5d235..1896a08 100644 --- a/src/local.ts +++ b/src/local.ts @@ -24,24 +24,16 @@ const optionDefinitions = [ name: "config", type: String, alias: "c", - description: "Path to the configuration file. (default: ./config.json)", + description: "Path to the JSON configuration file. (default: ./config.json)", defaultValue: "./config.json", - typeLabel: "" + typeLabel: "" }, { - name: "sfw_directory", + name: "directory", type: String, - alias: "s", - description: "The directory of (SFW) images for the bot to post. (default: ./sfw)", - defaultValue: "./images/sfw", - typeLabel: "" - }, - { - name: "nsfw_directory", - type: String, - alias: "n", - description: "The directory of (NSFW) images for the bot to post. If it chooses these, they will be marked sensitive. (default: ./nsfw)", - defaultValue: "./images/nsfw", + alias: "d", + description: "The directory of images to upload. (default: ./images)", + defaultValue: "./images", typeLabel: "" }, { @@ -73,53 +65,80 @@ if (args.help) { console.log(usage); exit(0); } + +if (args.verbose) { + console.log("Running in verbose mode."); + console.log(); +} + // JSON object read from config file const data = JSON.parse(fs.readFileSync("./config.json", "utf8")); -const sfw_files = fs.readdirSync(args.sfw_directory); -const nsfw_files = fs.readdirSync(args.nsfw_directory); +const sfw_files = fs.readdirSync(args.directory + "/sfw"); +const nsfw_files = fs.readdirSync(args.directory + "/nsfw"); 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 let image: fs.ReadStream; -let sensitive: boolean; +let sensitivity: boolean; if (random >= sfw_files.length) { - // Random Image is NSFW, mark it sensitive - image = fs.createReadStream(args.nsfw_directory + "/" + nsfw_files[ random - sfw_files.length ]); - sensitive = true; + // Image is NSFW, mark it sensitive + image = fs.createReadStream(args.directory + "/nsfw" + nsfw_files[ random - sfw_files.length ]) + .on("error", (err: Error) => { + console.log("Error reading image from NSFW directory: " + err.message); + if (args.verbose) { + console.error("--BEGIN FULL ERROR--"); + console.error(err); + } else + console.error("Run with -v to see the full error."); + exit(1); + }); + sensitivity = true; } else { // Image is SFW, mark it not sensitive - image = fs.createReadStream(args.sfw_directory + "/" + sfw_files[ random ]); - sensitive = false; + image = fs.createReadStream(args.directory + "/sfw/" + sfw_files[ random ]) + .on("error", (err: Error) => { + console.log("Error reading image from SFW directory:" + err.message); + if (args.verbose) { + console.error("--BEGIN FULL ERROR--"); + console.error(err); + } else + console.error("Run with -v to see the full error."); + exit(1); + }); + sensitivity = false; } - + const client = generator(data.type, data.instance, data.accessToken); client.uploadMedia(image).then((res: Response) => { client.postStatus(args.message, { media_ids: [ res.data.id ], visibility: "unlisted", - sensitive: sensitive + sensitive: sensitivity } ).then((res: Response) => { console.log("Successfully posted to " + data.instance); if (args.verbose) - console.log(console.log(res.data)); + console.log(res.data); exit(0); } ).catch((err: Error) => { - console.error("Error posting to " + data.instance); - console.error("Run with -v to see the full error."); - if (args.verbose) + console.error("Error posting to " + data.instance + ": " + err.message); + if (args.verbose) { + console.error("--BEGIN FULL ERROR--"); console.error(err); - exit(1); + } else + console.error("Run with -v to see the full error."); } ); }).catch((err: Error) => { - console.error("Error uploading image to " + data.instance); - console.error("Run with -v to see the full error."); - if (args.verbose) + console.error("Error uploading image to " + data.instance + ": " + err.message); + if (args.verbose) { + console.error("--BEGIN FULL ERROR--"); console.error(err); + } else + console.error("Run with -v to see the full error."); exit(1); }); diff --git a/yarn.lock b/yarn.lock index ff9d71b..08df468 100644 --- a/yarn.lock +++ b/yarn.lock @@ -539,10 +539,10 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: resolved "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== -fast-glob@^3.1.1: - version "3.2.7" - resolved "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.7.tgz#fd6cb7a2d7e9aa7a7846111e85a196d6b2f766a1" - integrity sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q== +fast-glob@^3.2.9: + version "3.2.9" + resolved "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.9.tgz#8f55f664b68a236bd29fa165817fc44f2b11faba" + integrity sha512-MBwILhhD92sziIrMQwpqcuGERF+BH99ei2a3XsGJuqEKcSycAL+w0HWokFenZXona+kjFr82Lf71eTxNRC06XQ== dependencies: "@nodelib/fs.stat" "^2.0.2" "@nodelib/fs.walk" "^1.2.3" @@ -659,15 +659,15 @@ globals@^13.6.0, globals@^13.9.0: type-fest "^0.20.2" globby@^11.0.4: - version "11.0.4" - resolved "https://registry.npmjs.org/globby/-/globby-11.0.4.tgz#2cbaff77c2f2a62e71e9b2813a67b97a3a3001a5" - integrity sha512-9O4MVG9ioZJ08ffbcyVYyLOJLk5JQ688pJ4eMGLpdWLHq/Wr1D9BlriLQyL0E+jbkuePVZXYFj47QM/v093wHg== + version "11.1.0" + resolved "https://registry.npmjs.org/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b" + integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g== dependencies: array-union "^2.1.0" dir-glob "^3.0.1" - fast-glob "^3.1.1" - ignore "^5.1.4" - merge2 "^1.3.0" + fast-glob "^3.2.9" + ignore "^5.2.0" + merge2 "^1.4.1" slash "^3.0.0" has-flag@^3.0.0: @@ -693,7 +693,7 @@ ignore@^4.0.6: resolved "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== -ignore@^5.1.4, ignore@^5.1.8: +ignore@^5.1.8, ignore@^5.2.0: version "5.2.0" resolved "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a" integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ== @@ -811,7 +811,7 @@ megalodon@^4.0.0: uuid "^8.0.0" ws "8.4.0" -merge2@^1.3.0: +merge2@^1.3.0, merge2@^1.4.1: version "1.4.1" resolved "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae" integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==