Sha256: 7a3f369b30be606dc27427ea49d7cfda9cf5be082e596a1b930522246d7ad863
Contents?: true
Size: 1.55 KB
Versions: 3
Compression:
Stored size: 1.55 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 [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 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_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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
hammer_cli-0.0.12 | lib/hammer_cli/exception_handler.rb |
hammer_cli-0.0.11 | lib/hammer_cli/exception_handler.rb |
hammer_cli-0.0.10 | lib/hammer_cli/exception_handler.rb |