Method: Faulty::Circuit#try_run

Defined in:
lib/faulty/circuit.rb

#try_run { ... } ⇒ Result<Object, Error>

Run the circuit as with #run, but return a Result

This is syntax sugar for running a circuit and rescuing an error

Examples:

result = Faulty.circuit(:api).try_run do
  api.get
end

response = if result.ok?
  result.get
else
  { error: result.error.message }
end
# The Result object has a fetch method that can return a default value
# if an error occurs
result = Faulty.circuit(:api).try_run do
  api.get
end.fetch({})

Parameters:

  • A cache key, or nil if caching is not desired

Yields:

  • The block to protect with this circuit

Returns:

  • A result where the ok value is the return value of the block, or the error value is an error captured by the circuit.

Raises:

  • If the block raises an error not in the error list, or if the error is excluded.



273
274
275
276
277
# File 'lib/faulty/circuit.rb', line 273

def try_run(...)
  Result.new(ok: run(...))
rescue FaultyError => e
  Result.new(error: e)
end