Module: BlockRepeater::Replay

Included in:
BlockRepeater
Defined in:
lib/block_repeater/replay.rb

Overview

Provides a retry-with-refresh mechanism for UI actions that may fail due to transient driver/page errors.

Consumers configure which exceptions to catch and how to refresh the page.

Instance Method Summary collapse

Instance Method Details

#replay(exceptions:, times: 3, delay: 0.5, refresh: nil, logger: nil, &block) ⇒ Object

Execute a block, retrying on configured exceptions with optional page refresh.

Parameters:

  • exceptions (Array<Class>)

    Exception types to catch and retry on

  • times (Integer) (defaults to: 3)

    Maximum retry attempts (default: 3)

  • delay (Float) (defaults to: 0.5)

    Seconds between retries (default: 0.5)

  • refresh (Proc, nil) (defaults to: nil)

    Callable to execute between retries for page refresh (default: nil)

  • logger (#error, nil) (defaults to: nil)

    Logger for error messages; nil to silence (default: nil)

  • &block
    • The block of code to retry

Returns:

  • The result of the block

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/block_repeater/replay.rb', line 20

def replay(exceptions:, times: 3, delay: 0.5, refresh: nil, logger: nil, &block)
  raise ArgumentError, 'replay requires a block' unless block

  attempt = 0

  repeat(times: times, delay: delay, &block).catch(exceptions: exceptions, behaviour: :continue) do |e|
    attempt += 1
    logger&.error { e.message }
    raise(e) if attempt >= times

    refresh&.call
  end.until do |result|
    !result.is_a?(Exception)
  end
end