Skip to content

Commit

Permalink
Rework exception handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
neomilium committed Apr 23, 2021
1 parent e22daf8 commit a31b0b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
16 changes: 12 additions & 4 deletions lib/modulesync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
require 'monkey_patches'

module ModuleSync # rubocop:disable Metrics/ModuleLength
class Error < StandardError; end

include Constants

def self.config_defaults
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 4 additions & 7 deletions lib/modulesync/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a31b0b8

Please sign in to comment.