Module: Serf::Util::ProtectedCall

Included in:
ErrorHandling
Defined in:
lib/serf/util/protected_call.rb

Overview

Rescues exceptions raised when calling blocks.

Instance Method Summary collapse

Instance Method Details

#pcall(*args) ⇒ Object Also known as: protected_call

A block wrapper to catch errors when executing a block. Instead of raising the error, the error is caught and returned in place of the block’s results.

ok, results = protected_call do
  1 + 1
end
=> [true, 2]

ok, results = protected_call do
  raise 'My Error'
end
=> [false, RuntimeError]

Returns:

  • boolean success and the block’s (or caught exception) results.



26
27
28
29
30
# File 'lib/serf/util/protected_call.rb', line 26

def pcall(*args)
  return yield(*args), nil
rescue => e
  return nil, e
end