Module: Porch::Rescuable

Defined in:
lib/porch/rescuable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



48
49
50
# File 'lib/porch/rescuable.rb', line 48

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#handle_exceptionsObject



3
4
5
6
7
# File 'lib/porch/rescuable.rb', line 3

def handle_exceptions
  yield if block_given?
rescue Exception => e
  rescue_with_handler(e) || raise(e)
end

#handler_for_rescue(exception) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/porch/rescuable.rb', line 9

def handler_for_rescue(exception)
  _, handler = self.class.rescue_handlers.detect do |exception_class, _|
    exception_class === exception
  end

  handler
end

#rescue_with_handler(exception) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/porch/rescuable.rb', line 17

def rescue_with_handler(exception)
  handler = handler_for_rescue(exception)
  unless handler.nil?
    case handler
    when Symbol
      self.method(handler).call exception
    when Proc
      self.instance_exec exception, &handler
    end

    exception
  end
end