Class: NotNaughty::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/not_naughty/error_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler = Kernel) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



20
21
22
# File 'lib/not_naughty/error_handler.rb', line 20

def initialize(handler = Kernel)
  @handles = Tree::TreeNode.new Exception, proc { |e| handler.raise e }
end

Instance Method Details

#handle(exception_class, &block) ⇒ Object

Inserts handle into the ordered tree.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/not_naughty/error_handler.rb', line 31

def handle(exception_class, &block)
  closest_handle = @handles.closest exception_class

  if closest_handle == exception_class then closest_handle.content = block
  else
    new_handle = Tree::TreeNode.new exception_class, block

    closest_handle.children do |child|
      exception_class > child.name and
      new_handle << closest_handle.remove!(child)
    end

    closest_handle << new_handle
  end
end

#raise(exception) ⇒ Object

Calls closest handle with exception.



25
26
27
28
# File 'lib/not_naughty/error_handler.rb', line 25

def raise(exception)
  handle = @handles.closest exception.class
  handle.content.call exception
end