Sha256: 7f30b183b7fb9ac1bb6c2530ca80854628bdf9b8f2130f4c8425c0cb64086ada

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module OptparsePlus
  # Standard exception you can throw to exit with a given 
  # status code. Generally, you should prefer OptparsePlus::Main#exit_now! over using
  # this directly, however you may wish to create a rich hierarchy of exceptions that extend from
  # this in your app, so this is provided if you wish to do so.
  class Error < StandardError
    attr_reader :exit_code
    # Create an Error with the given status code and message
    def initialize(exit_code,message=nil)
      super(message)
      @exit_code = exit_code
    end
  end

  # Thrown by certain methods when an externally-called command exits nonzero
  class FailedCommandError < Error

    # The command that caused the failure
    attr_reader :command

    # exit_code:: exit code of the command that caused this
    # command:: the entire command-line that caused this
    # custom_error_message:: an error message to show the user instead of the boilerplate one.  Useful
    #                        for allowing this exception to bubble up and exit the program, but to give
    #                        the user something actionable.
    def initialize(exit_code,command,custom_error_message = nil)
      error_message = String(custom_error_message).empty? ?  "Command '#{command}' exited #{exit_code}" : custom_error_message
      super(exit_code,error_message)
      @command = command
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
optparse-plus-3.0.1 lib/optparse_plus/error.rb
optparse-plus-3.0.0 lib/optparse_plus/error.rb