Sha256: c57e22535fb02f4615a50b27389eab5cd224263d89e007990e8a05064b74cccd

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

require 'command_kit/main'
require 'command_kit/printing'

module CommandKit
  #
  # 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
  #
  module ExceptionHandler
    include Printing

    #
    # Calls superclass'es `#main` method, but rescues any uncaught exceptions
    # and passes them to {#on_exception}.
    #
    # @param [Array<String>] argv
    #   The given arguments Array.
    #
    # @return [Integer]
    #   The exit status of the command.
    #
    # @api public
    #
    def main(argv=[])
      super(argv)
    rescue Interrupt, Errno::EPIPE => error
      raise(error)
    rescue Exception => error
      on_exception(error)
    end

    #
    # Default method for handling when an exception is raised by `#main`.
    #
    # @param [Exception] error
    #   The raised exception.
    #
    # @api semipublic
    #
    def on_exception(error)
      print_exception(error)
      exit(1)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
command_kit-0.5.0 lib/command_kit/exception_handler.rb
command_kit-0.4.1 lib/command_kit/exception_handler.rb
command_kit-0.4.0 lib/command_kit/exception_handler.rb
command_kit-0.3.0 lib/command_kit/exception_handler.rb
command_kit-0.2.2 lib/command_kit/exception_handler.rb
command_kit-0.2.1 lib/command_kit/exception_handler.rb
command_kit-0.2.0 lib/command_kit/exception_handler.rb
command_kit-0.1.0 lib/command_kit/exception_handler.rb