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



25
26
27
28
29
30
# File 'lib/hammer_cli/exception_handler.rb', line 25

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
21
22
23
# 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],
    [ApipieBindings::DocLoadingError, :handle_apipie_docloading_error],
    [ApipieBindings::MissingArgumentsError, :handle_apipie_missing_arguments_error],
    [HammerCLI::ModuleDisabledButRequired, :handle_generic_config_error]
  ]
end

#outputObject



32
33
34
# File 'lib/hammer_cli/exception_handler.rb', line 32

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