Class: Toys::CLI::DefaultErrorHandler

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

Overview

A basic error handler that prints out captured errors to a stream or a logger.

Instance Method Summary collapse

Constructor Details

#initialize(output: $stderr) ⇒ DefaultErrorHandler

Create an error handler.

Parameters:

  • output (IO, nil) (defaults to: $stderr)

    Where to write errors. Default is $stderr.



568
569
570
571
# File 'lib/toys/cli.rb', line 568

def initialize(output: $stderr)
  require "toys/utils/terminal"
  @terminal = Utils::Terminal.new(output: output)
end

Instance Method Details

#call(error) ⇒ Integer

The error handler routine. Prints out the error message and backtrace, and returns the correct result code.

Parameters:

  • error (Exception)

    The error that occurred.

Returns:

  • (Integer)

    The result code for the execution.



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# 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