lib/nanoc3/cli/error_handler.rb in nanoc3-3.2.0 vs lib/nanoc3/cli/error_handler.rb in nanoc3-3.2.1

- old
+ new

@@ -1,10 +1,12 @@ # encoding: utf-8 module Nanoc3::CLI # Catches errors and prints nice diagnostic messages, then exits. + # + # @api private class ErrorHandler # @option params [Nanoc3::CLI::Command, nil] command The command that is # currently being executed, or nil if there is none def initialize(params={}) @@ -43,10 +45,67 @@ rescue StandardError, ScriptError => e self.print_error(e) exit(1) end + # Prints the given error to stderr. Includes message, possible resolution + # (see {#resolution_for}), compilation stack, backtrace, etc. + # + # @param [Error] error The error that should be described + # + # @return [void] + def self.print_error(error) + self.new.print_error(error) + end + + # Prints the given error to stderr. Includes message, possible resolution + # (see {#resolution_for}), compilation stack, backtrace, etc. + # + # @param [Error] error The error that should be described + # + # @return [void] + def print_error(error) + # Header + $stderr.puts + $stderr.puts "Captain! We’ve been hit!" + + # Exception and resolution (if any) + $stderr.puts + $stderr.puts '=== MESSAGE:' + $stderr.puts + $stderr.puts "#{error.class}: #{error.message}" + resolution = self.resolution_for(error) + $stderr.puts "#{resolution}" if resolution + + # Compilation stack + $stderr.puts + $stderr.puts '=== COMPILATION STACK:' + $stderr.puts + if self.stack.empty? + $stderr.puts " (empty)" + else + self.stack.reverse.each do |obj| + if obj.is_a?(Nanoc3::ItemRep) + $stderr.puts " - [item] #{obj.item.identifier} (rep #{obj.name})" + else # layout + $stderr.puts " - [layout] #{obj.identifier}" + end + end + end + + # Backtrace + $stderr.puts + $stderr.puts '=== BACKTRACE:' + $stderr.puts + $stderr.puts error.backtrace.to_enum(:each_with_index).map { |item, index| " #{index}. #{item}" }.join("\n") + + # Issue link + $stderr.puts + $stderr.puts "If you believe this is a bug in nanoc, please do report it at" + $stderr.puts "<https://github.com/ddfreyne/nanoc/issues/new>--thanks!" + end + protected # @return [Boolean] true if debug output is enabled, false if not # # @see Nanoc3::CLI.debug? @@ -97,56 +156,9 @@ 'rubypants' => 'rubypants', 'sass' => 'sass', 'systemu' => 'systemu', 'w3c_validators' => 'w3c_validators' } - - # Prints the given error to stderr. Includes message, possible resolution - # (see {#resolution_for}), compilation stack, backtrace, etc. - # - # @param [Error] error The error that should be described - # - # @return [void] - def print_error(error) - # Header - $stderr.puts - $stderr.puts "Captain! We’ve been hit!" - - # Exception and resolution (if any) - $stderr.puts - $stderr.puts '=== MESSAGE:' - $stderr.puts - $stderr.puts "#{error.class}: #{error.message}" - resolution = self.resolution_for(error) - $stderr.puts "#{resolution}" if resolution - - # Compilation stack - $stderr.puts - $stderr.puts '=== COMPILATION STACK:' - $stderr.puts - if self.stack.empty? - $stderr.puts " (empty)" - else - self.stack.reverse.each do |obj| - if obj.is_a?(Nanoc3::ItemRep) - $stderr.puts " - [item] #{obj.item.identifier} (rep #{obj.name})" - else # layout - $stderr.puts " - [layout] #{obj.identifier}" - end - end - end - - # Backtrace - $stderr.puts - $stderr.puts '=== BACKTRACE:' - $stderr.puts - $stderr.puts error.backtrace.to_enum(:each_with_index).map { |item, index| " #{index}. #{item}" }.join("\n") - - # Issue link - $stderr.puts - $stderr.puts "If you believe this is a bug in nanoc, please do report it at" - $stderr.puts "<https://github.com/ddfreyne/nanoc/issues/new>--thanks!" - end # Attempts to find a resolution for the given error, or nil if no # resolution can be automatically obtained. # # @param [Error] error The error to find a resolution for