From 60a7356f8b9d108421342a2b28aae74ca12e3366 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sun, 27 Oct 2024 21:36:23 +0100 Subject: [PATCH] fix: add better handling of exceptions (parsing, arguments, options, ...) --- src/cli.cr | 11 ++++++++++- src/config.cr | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cli.cr b/src/cli.cr index 4366d07..2498ee3 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -30,13 +30,22 @@ module GX Parsers::RootParser.new.build(parser, breadcrumbs, @config) end pparser.parse(args) + rescue e : OptionParser::MissingOption + STDERR.puts "ERROR: #{e.message}".colorize(:red) + exit(1) end def run command = CommandFactory.create_command(@config, @config.mode) abort("ERROR: unknown command for mode #{@config.mode}") if command.nil? - command.try &.execute + command.execute + rescue e : ArgumentError + STDERR.puts "ERROR: #{e.message}".colorize(:red) + exit(1) + rescue e : Exception + STDERR.puts "ERROR: #{e.message}".colorize(:red) + exit(1) end end end diff --git a/src/config.cr b/src/config.cr index d56ac18..dfba077 100644 --- a/src/config.cr +++ b/src/config.cr @@ -102,7 +102,12 @@ module GX file_data = File.read(config_path) file_patched = Crinja.render(file_data, {"env" => ENV.to_h}) - root = Models::RootConfig.from_yaml(file_patched) + begin + root = Models::RootConfig.from_yaml(file_patched) + rescue ex : YAML::ParseException + STDERR.puts "Error parsing configuration file: #{ex.message}".colorize(:red) + exit(1) + end mount_point_base_safe = root.global.mount_point_base raise Models::InvalidMountpointError.new("Invalid global mount point") if mount_point_base_safe.nil?