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.



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

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.



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

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.



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

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