fix: add better handling of exceptions (parsing, arguments, options, ...)

This commit is contained in:
Glenn Y. Rolland 2024-10-27 21:36:23 +01:00
parent 8b0d64ad86
commit 60a7356f8b
2 changed files with 16 additions and 2 deletions

View file

@ -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

View file

@ -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?