module RemotelyExceptional::Handlers::PrioritizedHandler::ClassMethods

Public Instance Methods

===(exception) click to toggle source

Determines if any of the available handlers match the provided exception.

@param exception [Exception] An exception. @return [Boolean] Returns true if a handler is available that matches the

given exception. Returns false if none of the available handlers match the
given exception.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 20
def ===(exception)
  !!handler_for_exception(exception)
end
block_handlers() click to toggle source

Returns the Hash of block handlers by priority.

@return [Hash{Integer,Set}] The Hash of priorities and which block handlers

belong to those priorities.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 28
def block_handlers
  Thread.current["#{name}.block_handlers"] ||= Hash.new(&HASH_BUILDER)
end
default_priority() click to toggle source

The default priority level that should be used for handlers when no priority is provided.

@return [Integer] The default priority level.

# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 36
def default_priority
  const_get(:DEFAULT_PRIORITY)
end
handle(exception = $!, context = {}) click to toggle source

Finds the handler with the highest priority that matches the exception and uses this handler to handle the exception. If no handlers are available that match the given exception, the exception is re-raised.

@param exception [Exception] The exception to handle. @param context [Hash{Symbol=>Object}] An optional Hash of additional

contextual information about the exception.

@raise [exception] The given exception is reraised if no handler is found

that matches the given exception.

@return [Symbol] Returns a symbol indicating what action should be taken

to continue execution. Depending on the situation, valid values include:
[:continue, :raise, :retry]
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 52
def handle(exception = $!, context = {})
  context, exception = exception, $! if exception.is_a?(Hash)
  priority_handler = handler_for_exception(exception)
  raise exception if !priority_handler
  priority_handler.handle(exception, context)
end
handler_for_exception(exception) click to toggle source

Finds the handler with the highest priority that matches the given exception. Returns nil if no matching handler can be found.

@param exception [Exception] The exception to find a matching handler for. @return [RemotelyExceptional::Handler] Returns the handler with the

highest priority that matches the given exception. If no handler is found,
returns nil.

@return [nil] Returns nil if no matching handler could be found.

# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 67
def handler_for_exception(exception)
  prioritized_handlers.detect { |handler| handler === exception }
end
prioritized_handlers() click to toggle source

Returns an enumerator that yields block handlers and registered handlers in priority ASC, name ASC order. The collection is lazily generated, so changes to the sets of handlers may appear during traversal. If consistent state is necessary, force the returned enumerator to eagerly generate the full collection using to_a or similar.

@return [Enumerator<RemotelyExceptional::Handler>] An enumerator of all

known block handlers and registered handlers in priority ASC, name ASC
order.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 80
def prioritized_handlers
  Enumerator.new do |yielder|
    priorities = (registered_handlers.keys | block_handlers.keys).sort!
    priorities.uniq!
    priorities.each do |priority|
      if registered_handlers.key?(priority)
        collected_handlers = registered_handlers[priority].to_a
      end
      if block_handlers.key?(priority)
        temp_handlers = block_handlers[priority].to_a
        collected_handlers &&= collected_handlers.concat(temp_handlers)
        collected_handlers ||= temp_handlers
      end
      collected_handlers.sort_by!(&:name)
      collected_handlers.uniq!
      collected_handlers.each { |handler| yielder << handler }
    end
  end
end
register_handler(handler, options = {}) click to toggle source

Adds the given handler to the set of registered handlers. Optionally, a priority may be supplied. If no priority is supplied the {::default_priority default priority} is used.

@param handler [RemotelyExceptional::Handler] The handler that should be

registered.

@param options [Hash{Symbol=>Object}] A Hash of optional arguments. @option options [Integer] :priority ({::default_priority}) The priority of

the handler.

@return [Boolean] Returns true if the handler was successfully registered

for the given priority. Returns false if the handler was already registered
for the given priority.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 112
def register_handler(handler, options = {})
  priority = options[:priority] || default_priority
  !!registered_handlers[priority].add?(handler)
end
registered_handlers() click to toggle source

Returns the Hash of registered handlers by priority.

@return [Hash{Integer,Set<RemotelyExceptional::Handler>}] The Hash of

priorities and which handlers belong to those priorities.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 121
def registered_handlers
  Thread.current["#{name}.registered_handlers"] ||= Hash.new(&HASH_BUILDER)
end
remove_handler(handler, options = {}) click to toggle source

Removes the given handler. By default removes the handler from the {::default_priority default_priority}, but a :priority option may be supplied to remove the handler from a specified priority.

@param handler [RemotelyExceptional::Handler] The handler that should be

removed.

@param options [Hash{Symbol=>Object}] A Hash of optional arguments. @option options [Integer] :priority ({::default_priority}) The priority that

should be searched for the given handler.

@return [Boolean] Returns true if the handler was successfully removed for

the given priority. Returns false if the handler was not registered for
the given priority.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 137
def remove_handler(handler, options = {})
  priority = options[:priority] || default_priority
  registered_handlers.key?(priority) &&
    !!registered_handlers[priority].delete(handler)
end
reset_handlers!() click to toggle source

Clears all {::block_handlers block handlers} and {::registered_handlers registered handlers}.

@return [true]

# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 147
def reset_handlers!
  Thread.current["#{name}.registered_handlers"] = nil
  Thread.current["#{name}.block_handlers"] = nil
  true
end
with_handler(handler, options = {}) { || ... } click to toggle source

Registers a handler for the duration of the given block. By default registers the block at the {::default_priority default priority}, but a specific priority may be supplied as an option.

@param handler [RemotelyExceptional::Handler] The handler that should be

registered.

@param options [Hash{Symbol=>Object}] A Hash of optional arguments. @option options [Integer] :priority ({::default_priority}) The priority that

should be used to register the handler.

@raise [ArgumentError] if a block is not provided. @return [Boolean] Returns true if the block handler was successfully

registered for the given priority. Returns false if a matching block
handler was already registered for the given priority.
# File lib/remotely_exceptional/handlers/prioritized_handler.rb, line 166
def with_handler(handler, options = {})
  raise ArgumentError, "Block required!" unless block_given?

  priority = options[:priority] || default_priority
  if block_handlers[priority].add?(handler)
    added_handler = true
  end
  yield
ensure
  block_handlers[priority].delete(handler) if added_handler
end