Class: Briefly::ErrorRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/briefly/error_registry.rb

Overview

An ordered list of rescue_from registrations, queried at dispatch time.

Entries are stored oldest-first and returned newest-first, so the most recent registration for a given error class wins. Reads are lock-free against a frozen array; writes rebind it under a mutex, because rebinding alone would drop concurrent registrations.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeErrorRegistry

Returns a new instance of ErrorRegistry.



13
14
15
16
# File 'lib/briefly/error_registry.rb', line 13

def initialize
  @entries = [].freeze
  @mutex = Mutex.new
end

Instance Method Details

#add(klass, names, handler) ⇒ self

Parameters:

  • klass (Class)

    matched against the raised error and its subclasses

  • names (Array<Symbol>, nil)

    canonical shortcut names, or nil for every shortcut

  • handler (Proc)

    called with (error, shortcut_name)

Returns:

  • (self)


22
23
24
25
# File 'lib/briefly/error_registry.rb', line 22

def add(klass, names, handler)
  @mutex.synchronize { @entries = [*@entries, Entry.new(klass, names&.freeze, handler)].freeze }
  self
end

#clearself

Returns:

  • (self)


35
36
37
38
# File 'lib/briefly/error_registry.rb', line 35

def clear
  @mutex.synchronize { @entries = [].freeze }
  self
end

#scoped(name) ⇒ Array<Entry>

Returns handlers scoped to name, most recently registered first.

Parameters:

  • name (Symbol)

    a canonical shortcut name

Returns:

  • (Array<Entry>)

    handlers scoped to name, most recently registered first



29
# File 'lib/briefly/error_registry.rb', line 29

def scoped(name) = @entries.reverse_each.select { |entry| entry.names&.include?(name) }

#wideArray<Entry>

Returns handlers scoped to no shortcut in particular, most recent first.

Returns:

  • (Array<Entry>)

    handlers scoped to no shortcut in particular, most recent first



32
# File 'lib/briefly/error_registry.rb', line 32

def wide = @entries.reverse_each.select { |entry| entry.names.nil? }