From 2b5d2a36438d286ec7e7b4a155b5b7c005b27a0c Mon Sep 17 00:00:00 2001 From: Kallum Jones Date: Wed, 3 Aug 2022 17:30:47 +0100 Subject: [PATCH] Make error handling clearer in Mods --- src/mods/mods.ts | 7 +++++-- src/util/print_utils.ts | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/mods/mods.ts b/src/mods/mods.ts index add928a..5b70152 100644 --- a/src/mods/mods.ts +++ b/src/mods/mods.ts @@ -28,7 +28,9 @@ export default class Mods { if (e instanceof ModNotFoundError) { PrintUtils.info(`Mod not found on ${source.getName()}`); } else { - throw e; + PrintUtils.error(`An error occurred searching for ${mod} on ${source.getName()}. Skipping ${source.getName()}`, e) + // Try the next source + continue; } } @@ -39,7 +41,8 @@ export default class Mods { await source.install(id); PrintUtils.success(`Successfully installed ${mod}`); } catch (e) { - PrintUtils.error(`An error occurred while downloading ${mod} from ${source.getName()}`, e); + // Log the error, and continue to next source + PrintUtils.error(e); } } } diff --git a/src/util/print_utils.ts b/src/util/print_utils.ts index dfcc998..ef889d8 100644 --- a/src/util/print_utils.ts +++ b/src/util/print_utils.ts @@ -52,9 +52,22 @@ export default class PrintUtils { console.log(chalk.greenBright(print)); } - static error(print: string, err?: Error) { - console.log(chalk.redBright(print)); + static error(print: string | Error, err?: Error) { + // If provided an error + if (print instanceof Error) { + // Output the error message + console.log(chalk.redBright(print.message)); + // If no accompanying error to log was passed, log this one + if (err == null) { + err = print; + } + } else { + // If a string is provided, output to user + console.log(chalk.redBright(print)); + } + + // If there is an error to log, log it if (err instanceof Error) { if (ModManager.logger != null) { ModManager.logger.error(err)