Module: SmartCore::Engine::RescueExt

Defined in:
lib/smart_core/engine/rescue_ext.rb

Overview

Since:

  • 0.6.0

Class Method Summary collapse

Class Method Details

.inline_rescue_pipe(*proks, &error_interceptor) ⇒ Any

rubocop:disable Performance/RedundantBlockCall

Parameters:

  • proks (Array<Proc>)
  • error_interceptor (Block)

Returns:

  • (Any)

Since:

  • 0.6.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/smart_core/engine/rescue_ext.rb', line 19

def inline_rescue_pipe(*proks, &error_interceptor)
  unless proks.all? { |prok| prok.is_a?(::Proc) }
    raise(SmartCore::ArgumentError, 'Invalid proc object')
  end

  interceptable_bloks = proks.to_enum
  pipe_invokation_result = nil
  last_exception = nil

  begin
    while current_block = interceptable_bloks.next
      begin
        pipe_invokation_result = current_block.call
        break
      rescue => error
        last_exception = error
      end
    end

    pipe_invokation_result
  rescue ::StopIteration
    error_interceptor ? error_interceptor.call(last_exception) : raise(last_exception)
  end
end