Class: Nanoc::CLI::ErrorHandler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/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.

API:

  • private

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disableObject

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.

Disables error handling. This is used by the test cases to prevent error from being handled by the CLI while tests are running.

API:

  • private



21
22
23
# File 'lib/nanoc/cli/error_handler.rb', line 21

def self.disable
  @disabled = true
end

.enableObject

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.

Re-enables error handling after it was disabled. This is used by the test cases to prevent error from being handled by the CLI while tests are running.

API:

  • private



28
29
30
# File 'lib/nanoc/cli/error_handler.rb', line 28

def self.enable
  @disabled = false
end

.handle_while(exit_on_error: true) ⇒ 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.

API:

  • private



11
12
13
14
15
16
17
# File 'lib/nanoc/cli/error_handler.rb', line 11

def self.handle_while(exit_on_error: true, &)
  if @disabled
    yield
  else
    new.handle_while(exit_on_error:, &)
  end
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:

  • The error that should be described

API:

  • private



91
92
93
# File 'lib/nanoc/cli/error_handler.rb', line 91

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

Instance Method Details

#handle_error(error, exit_on_error:) ⇒ Object

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.

API:

  • private



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nanoc/cli/error_handler.rb', line 70

def handle_error(error, exit_on_error:)
  if trivial?(error)
    $stderr.puts
    $stderr.puts "Error: #{error.message}"
    resolution = resolution_for(error)
    if resolution
      $stderr.puts
      $stderr.puts resolution
    end
  else
    print_error(error)
  end
  exit(1) if exit_on_error
end

#handle_while(exit_on_error:) ⇒ 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.

API:

  • private



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nanoc/cli/error_handler.rb', line 36

def handle_while(exit_on_error:)
  # Set exit handler
  %w[INT TERM].each do |signal|
    Signal.trap(signal) do
      puts
      exit(0)
    end
  end

  # Set stack trace dump handler
  if !defined?(RUBY_ENGINE) || RUBY_ENGINE != 'jruby'
    begin
      Signal.trap('USR1') do
        puts 'Caught USR1; dumping a stack trace'
        puts caller.map { |i| "  #{i}" }.join("\n")
      end
    rescue ArgumentError
    end
  end

  # Run
  yield
rescue Exception => e # rubocop:disable Lint/RescueException
  case e
  when Interrupt
    puts
    exit(1)
  when StandardError, ScriptError
    handle_error(e, exit_on_error:)
  else
    raise e
  end
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:

  • The error that should be described

API:

  • private



101
102
103
104
105
106
107
# File 'lib/nanoc/cli/error_handler.rb', line 101

def print_error(error)
  write_compact_error(error, $stderr)

  File.open('crash.log', 'w') do |io|
    write_verbose_error(error, io)
  end
end

#trivial?(error) ⇒ Boolean

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:

API:

  • private



152
153
154
155
156
157
158
159
160
161
# File 'lib/nanoc/cli/error_handler.rb', line 152

def trivial?(error)
  case error
  when Nanoc::Core::TrivialError, Errno::EADDRINUSE
    true
  when LoadError
    GEM_NAMES.key?(gem_name_from_load_error(error))
  else
    false
  end
end

#write_compact_error(error, stream) ⇒ 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.

Writes a compact representation of the error, suitable for a terminal, on the given stream (probably stderr).

Parameters:

  • The error that should be described

  • The stream to write the description too

API:

  • private



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nanoc/cli/error_handler.rb', line 117

def write_compact_error(error, stream)
  stream.puts
  stream.puts 'Captain! We’ve been hit!'

  write_error_message(stream, error)
  write_error_detail(stream, error)
  write_item_rep(stream, error)
  write_stack_trace(stream, error)

  stream.puts
  stream.puts 'A detailed crash log has been written to ./crash.log.'
end

#write_verbose_error(error, stream) ⇒ 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.

Writes a verbose representation of the error on the given stream.

Parameters:

  • The error that should be described

  • The stream to write the description too

API:

  • private



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nanoc/cli/error_handler.rb', line 137

def write_verbose_error(error, stream)
  stream.puts "Crash log created at #{Time.now}"

  write_error_message(stream, error, verbose: true)
  write_error_detail(stream, error)
  write_item_rep(stream, error, verbose: true)
  write_stack_trace(stream, error, verbose: true)
  write_version_information(stream, verbose: true)
  write_system_information(stream, verbose: true)
  write_installed_gems(stream, verbose: true)
  write_gemfile_lock(stream, verbose: true)
  write_load_paths(stream, verbose: true)
end