Class: HammerCLI::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/exception_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ExceptionHandler

Returns a new instance of ExceptionHandler.



7
8
9
10
# File 'lib/hammer_cli/exception_handler.rb', line 7

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

Instance Method Details

#handle_exception(e, options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/hammer_cli/exception_handler.rb', line 22

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

#mappingsObject



12
13
14
15
16
17
18
19
20
# File 'lib/hammer_cli/exception_handler.rb', line 12

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

#outputObject



29
30
31
# File 'lib/hammer_cli/exception_handler.rb', line 29

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