Class: Nanoc3::CLI::ErrorHandler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/cli/error_handler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Catches errors and prints nice diagnostic messages, then exits.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ErrorHandler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ErrorHandler.

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • command (Nanoc3::CLI::Command, nil)

    The command that is currently being executed, or nil if there is none



12
13
14
# File 'lib/nanoc3/cli/error_handler.rb', line 12

def initialize(params={})
  @command = params[:command]
end

Class Method Details

.handle_while(params = {}, &block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Enables error handling in the given block.

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • command (Nanoc3::CLI::Command, nil)

    The command that is currently being executed, or nil if there is none



22
23
24
# File 'lib/nanoc3/cli/error_handler.rb', line 22

def self.handle_while(params={}, &block)
  self.new(params).handle_while(&block)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Prints the given error to stderr. Includes message, possible resolution (see #resolution_for), compilation stack, backtrace, etc.

Parameters:

  • error (Error)

    The error that should be described



56
57
58
# File 'lib/nanoc3/cli/error_handler.rb', line 56

def self.print_error(error)
  self.new.print_error(error)
end

Instance Method Details

#handle_while(&block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Enables error handling in the given block. This method should not be called directly; use handle_while instead.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nanoc3/cli/error_handler.rb', line 32

def handle_while(&block)
  # Set exit handler
  [ 'INT', 'TERM' ].each do |signal|
    Signal.trap(signal) do
      puts
      exit!(0)
    end
  end

  # Run
  yield
rescue Interrupt => e
  exit(1)
rescue StandardError, ScriptError => e
  self.print_error(e)
  exit(1)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Prints the given error to stderr. Includes message, possible resolution (see #resolution_for), compilation stack, backtrace, etc.

Parameters:

  • error (Error)

    The error that should be described



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nanoc3/cli/error_handler.rb', line 66

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