Sha256: 846933072831e708d7822a11ec332867f62f98d1b3a7f83b8549abb41086a0f8

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 KB

Contents

require 'rest_client'
require 'logging'

module HammerCLI
  class ExceptionHandler

    def initialize(options={})
      @logger = Logging.logger['Exception']
      @output = options[:output]
    end

    def mappings
      [
        [Exception, :handle_general_exception], # catch all
        [Clamp::HelpWanted, :handle_help_wanted],
        [Clamp::UsageError, :handle_usage_exception],
        [RestClient::ResourceNotFound, :handle_not_found],
        [RestClient::Unauthorized, :handle_unauthorized],
      ]
    end

    def handle_exception(e, options={})
      @options = options
      handler = mappings.reverse.find { |m| e.class.respond_to?(:"<=") ? e.class <= m[0] : false }
      return send(handler[1], e) if handler
      raise e
    end

    def output
      @output || HammerCLI::Output::Output.new
    end

    protected

    def print_error(error)
      error = error.join("\n") if error.kind_of? Array
      @logger.error error

      if @options[:heading]
        output.print_error(@options[:heading], error)
      else
        output.print_error(error)
      end
    end

    def print_message(msg)
      output.print_message(msg)
    end

    def log_full_error(e)
      backtrace = e.backtrace || []
      @logger.error "\n\n#{e.class} (#{e.message}):\n    " +
        backtrace.join("\n    ")
        "\n\n"
    end

    def handle_general_exception(e)
      print_error "Error: " + e.message
      log_full_error e
      HammerCLI::EX_SOFTWARE
    end

    def handle_usage_exception(e)
      print_error "Error: %s\n\nSee: '%s --help'" % [e.message, e.command.invocation_path]
      log_full_error e
      HammerCLI::EX_USAGE
    end

    def handle_help_wanted(e)
      print_message e.command.help
      HammerCLI::EX_OK
    end

    def handle_not_found(e)
      print_error e.message
      log_full_error e
      HammerCLI::EX_NOT_FOUND
    end

    def handle_unauthorized(e)
      print_error "Invalid username or password"
      log_full_error e
      HammerCLI::EX_UNAUTHORIZED
    end

  end
end



Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hammer_cli-0.0.18 lib/hammer_cli/exception_handler.rb
hammer_cli-0.0.16 lib/hammer_cli/exception_handler.rb
hammer_cli-0.0.15 lib/hammer_cli/exception_handler.rb
hammer_cli-0.0.14 lib/hammer_cli/exception_handler.rb
hammer_cli-0.0.13 lib/hammer_cli/exception_handler.rb