Class: Nanoc::CLI::ErrorHandler Private
- Inherits:
-
Object
- Object
- Nanoc::CLI::ErrorHandler
- 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.
Class Method Summary collapse
-
.disable ⇒ Object
private
Disables error handling.
-
.enable ⇒ Object
private
Re-enables error handling after it was disabled.
-
.handle_while(command: nil, &block) ⇒ void
private
Enables error handling in the given block.
-
.print_error(error) ⇒ void
private
Prints the given error to stderr.
Instance Method Summary collapse
-
#handle_while(&_block) ⇒ void
private
Enables error handling in the given block.
-
#initialize(command: nil) ⇒ ErrorHandler
constructor
private
A new instance of ErrorHandler.
-
#print_error(error) ⇒ void
private
Prints the given error to stderr.
-
#write_compact_error(error, stream) ⇒ void
private
Writes a compact representation of the error, suitable for a terminal, on the given stream (probably stderr).
-
#write_verbose_error(error, stream) ⇒ void
private
Writes a verbose representation of the error on the given stream.
Constructor Details
#initialize(command: nil) ⇒ 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.
8 9 10 |
# File 'lib/nanoc/cli/error_handler.rb', line 8 def initialize(command: nil) @command = command end |
Class Method Details
.disable ⇒ 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.
Disables error handling. This is used by the test cases to prevent error from being handled by the CLI while tests are running.
28 29 30 |
# File 'lib/nanoc/cli/error_handler.rb', line 28 def self.disable @disabled = true end |
.enable ⇒ 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.
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.
35 36 37 |
# File 'lib/nanoc/cli/error_handler.rb', line 35 def self.enable @disabled = false end |
.handle_while(command: nil, &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.
18 19 20 21 22 23 24 |
# File 'lib/nanoc/cli/error_handler.rb', line 18 def self.handle_while(command: nil, &block) if @disabled yield else new(command: command).handle_while(&block) end end |
.print_error(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.
Prints the given error to stderr. Includes message, possible resolution (see #resolution_for), compilation stack, backtrace, etc.
81 82 83 |
# File 'lib/nanoc/cli/error_handler.rb', line 81 def self.print_error(error) 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.
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 69 70 71 72 73 |
# File 'lib/nanoc/cli/error_handler.rb', line 43 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::Int::Errors::GenericTrivial => e $stderr.puts "Error: #{e.message}" exit(1) rescue Interrupt exit(1) rescue StandardError, ScriptError => e print_error(e) exit(1) end |
#print_error(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.
Prints the given error to stderr. Includes message, possible resolution (see #resolution_for), compilation stack, backtrace, etc.
91 92 93 94 95 96 97 98 99 |
# File 'lib/nanoc/cli/error_handler.rb', line 91 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 |
#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).
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/nanoc/cli/error_handler.rb', line 109 def write_compact_error(error, stream) # Header stream.puts stream.puts 'Captain! We’ve been hit!' # Sections (stream, error) write_compilation_stack(stream, error) write_stack_trace(stream, error) # Issue link write_issue_link(stream) 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/nanoc/cli/error_handler.rb', line 130 def write_verbose_error(error, stream) # Header stream.puts "Crashlog created at #{Time.now}" # Sections (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_gemfile_lock(stream, verbose: true) write_load_paths(stream, verbose: true) end |