class Toys::CLI::DefaultErrorHandler
A basic error handler that prints out captured errors to a stream or a logger.
Public Class Methods
new(output: $stderr)
click to toggle source
Create an error handler.
@param output [IO,nil] Where to write errors. Default is `$stderr`.
# File lib/toys/cli.rb, line 568 def initialize(output: $stderr) require "toys/utils/terminal" @terminal = Utils::Terminal.new(output: output) end
Public Instance Methods
call(error)
click to toggle source
The error handler routine. Prints out the error message and backtrace, and returns the correct result code.
@param error [Exception] The error that occurred. @return [Integer] The result code for the execution.
# File lib/toys/cli.rb, line 580 def call(error) cause = error case error when ContextualError cause = error.cause @terminal.puts(cause_string(cause)) @terminal.puts(context_string(error), :bold) when ::Interrupt @terminal.puts @terminal.puts("INTERRUPTED", :bold) else @terminal.puts(cause_string(error)) end exit_code_for(cause) end
Private Instance Methods
cause_string(cause)
click to toggle source
# File lib/toys/cli.rb, line 611 def cause_string(cause) lines = ["#{cause.class}: #{cause.message}"] cause.backtrace.each_with_index.reverse_each do |bt, i| lines << " #{(i + 1).to_s.rjust(3)}: #{bt}" end lines.join("\n") end
context_string(error)
click to toggle source
# File lib/toys/cli.rb, line 619 def context_string(error) lines = [ error.banner || "Unexpected error!", " #{error.cause.class}: #{error.cause.message}", ] if error.config_path lines << " in config file: #{error.config_path}:#{error.config_line}" end if error.tool_name lines << " while executing tool: #{error.tool_name.join(' ').inspect}" if error.tool_args lines << " with arguments: #{error.tool_args.inspect}" end end lines.join("\n") end
exit_code_for(error)
click to toggle source
# File lib/toys/cli.rb, line 598 def exit_code_for(error) case error when ArgParsingError 2 when NotRunnableError 126 when ::Interrupt 130 else 1 end end