From 4a432e6a23e578cff349b31fa42a6b7c76541763 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Fri, 27 Sep 2024 21:29:01 +0530 Subject: [PATCH] nicer error handling --- tribes-cli/cli.ts | 7 ++++++- tribes-cli/utils/command.ts | 12 ++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tribes-cli/cli.ts b/tribes-cli/cli.ts index dc1949b1..397fcce3 100644 --- a/tribes-cli/cli.ts +++ b/tribes-cli/cli.ts @@ -27,5 +27,10 @@ async function main() { } if (import.meta.main) { - await main(); + try { + await main(); + } catch (e) { + console.error('ERROR:', e.message); + Deno.exit(1); + } } diff --git a/tribes-cli/utils/command.ts b/tribes-cli/utils/command.ts index 6ea1ec63..b6acc161 100644 --- a/tribes-cli/utils/command.ts +++ b/tribes-cli/utils/command.ts @@ -12,26 +12,22 @@ export class Command { this.description = description; } - get subcommandCount() { - return Object.keys(this.commands).length; - } - isValidSubcommand(key: string): boolean { return Object.keys(this.commands).includes(key.toString()); } getSubcommand(subcommand: string | number) { if (!subcommand) { - throw new Error('tribes-cli: error: invalid subcommand'); + throw new Error('tribes-cli: invalid subcommand'); } if (typeof subcommand === 'number') { - throw new Error('tribes-cli: error: subcommand cannot be a number'); + throw new Error('tribes-cli: subcommand cannot be a number'); } if (this.isValidSubcommand(subcommand)) { return this.commands[subcommand]; } else { - throw new Error(`tribes-cli: error: ${subcommand} is not a valid subcommand`); + throw new Error(`tribes-cli: ${subcommand} is not a valid subcommand`); } } @@ -42,7 +38,7 @@ export class Command { subcommand(name: string, command: Command) { if (this.isValidSubcommand(name)) { - throw new Error(`tribes-cli: error: ${name} is already a subcommand.`); + throw new Error(`tribes-cli: ${name} is already a subcommand.`); } this.commands[name] = command; return this;