From a31b0b8a59107a541b2411941f8ef1ffea69187d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 31 Dec 2020 20:53:30 +0100 Subject: [PATCH] Rework exception handling This commit gives the user a better output message: - it uses the exception message if available or fallback on the "Error during update" message - it prefixes the message by the current module name This commits introduces a ModuleSync::Error that will be used to raise exceptions, with a message outputed on stderr, but without a full backtrace, useless for user. It keeps the current behavior when error (ie. StandardError) comes from templates (ie. ERB) rendering as user may need to see why the rendering failed. Git::GitExecuteError backtraces are now hidden too: user needs to understand what happends, with the error message, but not where error occured. --- lib/modulesync.rb | 16 ++++++++++++---- lib/modulesync/repository.rb | 11 ++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/modulesync.rb b/lib/modulesync.rb index 8a9abfdd..8b7c468b 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -12,6 +12,8 @@ require 'monkey_patches' module ModuleSync # rubocop:disable Metrics/ModuleLength + class Error < StandardError; end + include Constants def self.config_defaults @@ -98,8 +100,8 @@ def self.manage_file(puppet_module, filename, settings, options) } template = Renderer.render(erb, configs, metadata) Renderer.sync(template, target_file) - rescue # rubocop:disable Lint/RescueWithoutErrorClass - $stderr.puts "Error while rendering #{filename}" + rescue StandardError => e + $stderr.puts "#{puppet_module.given_name}: Error while rendering file: '#{filename}'" raise end end @@ -173,8 +175,14 @@ def self.update(cli_options) managed_modules.each do |puppet_module| begin manage_module(puppet_module, module_files, defaults) - rescue # rubocop:disable Lint/RescueWithoutErrorClass - warn "Error while updating '#{puppet_module.given_name}'" + rescue ModuleSync::Error, Git::GitExecuteError => e + message = e.message || "Error during '#{options[:command]}'" + message = "#{puppet_module.given_name}: #{message}" + $stderr.puts message + exit 1 unless options[:skip_broken] + errors = true + $stdout.puts "Skipping '#{puppet_module.given_name}' as update process failed" + rescue StandardError => e raise unless options[:skip_broken] errors = true $stdout.puts "Skipping '#{puppet_module.given_name}' as update process failed" diff --git a/lib/modulesync/repository.rb b/lib/modulesync/repository.rb index f5c1f8f9..cd103a37 100644 --- a/lib/modulesync/repository.rb +++ b/lib/modulesync/repository.rb @@ -120,13 +120,10 @@ def submit_changes(files, options) repo.push('origin', branch, opts_push) end rescue Git::GitExecuteError => e - if e.message.match?(/working (directory|tree) clean/) - puts "There were no changes in '#{@directory}'. Not committing." - return false - else - puts e - raise - end + raise unless e.message.match?(/working (directory|tree) clean/) + + puts "There were no changes in '#{@directory}'. Not committing." + return false end true