Module: CommandKit::ExceptionHandler

Includes:
Printing
Included in:
BugReport, Command
Defined in:
lib/command_kit/exception_handler.rb

Overview

Adds exception handling and printing.

Examples

include CommandKit::Main
include CommandKit::ExceptionHandler

Custom Exception Handling

include CommandKit::Main
include CommandKit::ExceptionHandler

def on_exception(error)
  print_error "error: #{error.message}"
  exit(1)
end

Constant Summary

Constants included from Printing

Printing::EOL

Instance Method Summary collapse

Methods included from Printing

#print_error, #print_exception

Methods included from Stdio

#abort, #gets, #initialize, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout

Instance Method Details

#main(argv = []) ⇒ Integer

Calls superclass'es #main method, but rescues any uncaught exceptions and passes them to #on_exception.

Parameters:

  • argv (Array<String>) (defaults to: [])

    The given arguments Array.

Returns:

  • (Integer)

    The exit status of the command.



40
41
42
43
44
45
46
# File 'lib/command_kit/exception_handler.rb', line 40

def main(argv=[])
  super(argv)
rescue Interrupt, Errno::EPIPE => error
  raise(error)
rescue Exception => error
  on_exception(error)
end

#on_exception(error) ⇒ Object

Default method for handling when an exception is raised by #main.

Parameters:

  • error (Exception)

    The raised exception.



56
57
58
59
# File 'lib/command_kit/exception_handler.rb', line 56

def on_exception(error)
  print_exception(error)
  exit(1)
end