class HammerCLI::ExceptionHandler

Public Class Methods

new(options={}) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 7
def initialize(options={})
  @logger = Logging.logger['Exception']
  @output = options[:output]
end

Public Instance Methods

handle_exception(e, options={}) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 27
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
mappings() click to toggle source
# 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],
    [RestClient::SSLCertificateNotVerified, :handle_ssl_cert_not_verified],
    [OpenSSL::SSL::SSLError, :handle_ssl_error],
    [ApipieBindings::DocLoadingError, :handle_apipie_docloading_error],
    [ApipieBindings::MissingArgumentsError, :handle_apipie_missing_arguments_error],
    [HammerCLI::ModuleDisabledButRequired, :handle_generic_config_error]
  ]
end
output() click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 34
def output
  @output || HammerCLI::Output::Output.new
end

Protected Instance Methods

handle_apipie_docloading_error(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 118
def handle_apipie_docloading_error(e)
  api_cache_instructions = "\n  - " +
                           _("is the server down?") + "\n  - " +
                           _("was '%s' run on the server when using apipie cache? (typical production settings)") % rake_command

  message = _("Could not load the API description from the server") + ": "
  if e.respond_to?(:original_error)
    if e.original_error.is_a?(RestClient::SSLCertificateNotVerified)
      message += ssl_cert_message + "\n\n"
    else
      message += "\n#{e.original_error.message}"
      message += api_cache_instructions
    end
  else
    message += api_cache_instructions
  end
  print_error(message)
  log_full_error(e)
  HammerCLI::EX_CONFIG
end
handle_apipie_missing_arguments_error(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 139
def handle_apipie_missing_arguments_error(e)
  message = _("Missing arguments for %s") % "'#{e.params.join("', '")}'"
  print_error message
  log_full_error e, message
  HammerCLI::EX_USAGE
end
handle_general_exception(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 63
def handle_general_exception(e)
  print_error _("Error: %s") % e.message
  log_full_error e
  HammerCLI::EX_SOFTWARE
end
handle_generic_config_error(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 146
def handle_generic_config_error(e)
  print_error e.message
  log_full_error e
  HammerCLI::EX_CONFIG
end
handle_help_wanted(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 76
def handle_help_wanted(e)
  print_message e.command.help
  HammerCLI::EX_OK
end
handle_not_found(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 81
def handle_not_found(e)
  print_error e.message
  log_full_error e
  HammerCLI::EX_NOT_FOUND
end
handle_ssl_cert_not_verified(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 103
def handle_ssl_cert_not_verified(e)
  print_error(ssl_cert_message)
  log_full_error(e)
  HammerCLI::EX_CONFIG
end
handle_ssl_error(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 97
def handle_ssl_error(e)
  print_error(_("SSL error") + ": #{e.message}")
  log_full_error(e)
  HammerCLI::EX_CONFIG
end
handle_unauthorized(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 87
def handle_unauthorized(e)
  print_error e.message
  log_full_error e
  HammerCLI::EX_UNAUTHORIZED
end
handle_usage_exception(e) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 69
def handle_usage_exception(e)
  print_error (_("Error: %{message}") + "\n\n" +
               _("See: '%{path} --help'")) % {:message => e.message, :path => e.command.invocation_path}
  log_full_error e
  HammerCLI::EX_USAGE
end
log_full_error(e, message = e.message) click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 55
def log_full_error(e, message = e.message)
  backtrace = e.backtrace || []
  error = "\n\n#{e.class} (#{message}):\n    " +
    backtrace.join("\n    ") +
    "\n\n"
  @logger.error error
end
print_error(error) click to toggle source
print_message(msg) click to toggle source
rake_command() click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 93
def rake_command
  "rake apipie:cache"
end
ssl_cert_instructions() click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 109
def ssl_cert_instructions
end
ssl_cert_message() click to toggle source
# File lib/hammer_cli/exception_handler.rb, line 112
def ssl_cert_message
  message = _("SSL certificate verification failed")
  message += "\n#{ssl_cert_instructions}" if ssl_cert_instructions
  message
end