Make error handling more specific
Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
parent
26bcdc2df7
commit
80c6e925dd
3 changed files with 16 additions and 18 deletions
|
@ -13,9 +13,6 @@ async function readConfig(): Promise<string> {
|
|||
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<string> {
|
|||
* // Prints "https://mastodon.social"
|
||||
*/
|
||||
export default async function getConfig(): Promise<config> {
|
||||
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<config> {
|
|||
})
|
||||
)
|
||||
);
|
||||
} 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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in a new issue