# encoding: utf-8 module LocalPac class ErrorHandler @handlers = [] @mutex = Mutex.new class << self attr_reader :handlers, :mutex end private attr_reader :message, :exit_code public attr_reader :exception attr_accessor :original_message def initialize(options = {}) @exception = options.fetch(:exception) @message = options.fetch(:message) @exit_code = options.fetch(:exit_code) rescue KeyError => e raise ArgumentError, e.message end class << self def create(options = {}, &block) handler = new(options, &block) handlers << handler handler end def find(exception) handlers.find(proc { default_handler }) { |h| h.exception == exception } end private def default_handler mutex.synchronize do @default_handler ||= new( exception: StandardError, message: 'Sorry, but I cannot fullfil your request. An unexpected error occured.', exit_code: 99, ) end end end def run LocalPac.ui_logger.fatal message LocalPac.ui_logger.debug original_message if original_message Kernel.exit exit_code end end end