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.

Constant Summary

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ErrorHandler) initialize(params = {})

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 (Nanoc::CLI::Command, nil)

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



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

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

Class Method Details

+ (Object) disable

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.



34
35
36
# File 'lib/nanoc/cli/error_handler.rb', line 34

def self.disable
  @disabled = true
end

+ (Object) enable

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.



43
44
45
# File 'lib/nanoc/cli/error_handler.rb', line 43

def self.enable
  @disabled = false
end

+ (void) handle_while(params = {}, &block)

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 (Nanoc::CLI::Command, nil)

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



22
23
24
25
26
27
28
# File 'lib/nanoc/cli/error_handler.rb', line 22

def self.handle_while(params = {}, &block)
  if @disabled
    yield
  else
    new(params).handle_while(&block)
  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:

  • error (Error)

    The error that should be described



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

- (void) handle_while(&block)

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nanoc/cli/error_handler.rb', line 53

def handle_while(&block)
  # 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 Nanoc::Errors::GenericTrivial => e
  $stderr.puts "Error: #{e.message}"
  exit(1)
rescue Interrupt
  exit(1)
rescue StandardError, ScriptError => e
  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



101
102
103
104
105
106
107
108
109
# 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|
    cio = Nanoc::CLI.wrap_in_cleaning_stream(io)
    cio.add_stream_cleaner(::Nanoc::CLI::StreamCleaners::ANSIColors)
    write_verbose_error(error, cio)
  end
end

- (void) write_compact_error(error, stream)

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:

  • error (Error)

    The error that should be described

  • stream (IO)

    The stream to write the description too



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nanoc/cli/error_handler.rb', line 121

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

  # Sections
  write_error_message(stream, error)
  write_compilation_stack(stream, error)
  write_stack_trace(stream, error)

  # Issue link
  write_issue_link(stream)
end

- (void) write_verbose_error(error, stream)

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:

  • error (Error)

    The error that should be described

  • stream (IO)

    The stream to write the description too



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/nanoc/cli/error_handler.rb', line 144

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

  # Sections
  write_error_message(stream, error, :verbose => true)
  write_compilation_stack(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_environment(stream,        :verbose => true)
  write_gemfile_lock(stream,        :verbose => true)
  write_load_paths(stream,        :verbose => true)
end