module SmartCore::Engine::RescueExt

@api public @since 0.6.0

Public Instance Methods

inline_rescue_pipe(*proks, &error_interceptor) click to toggle source

@param proks [Array<Proc>] @param error_interceptor [Block] @return [Any]

@api public @since 0.6.0 rubocop:disable Performance/RedundantBlockCall

# 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