Module: Imap::Backup::RetryOnError

Included in:
Account::Folder, Client::AutomaticLoginWrapper
Defined in:
lib/imap/backup/retry_on_error.rb

Overview

Provides a mechanism for retrying blocks of code which often throw errors

Instance Method Summary collapse

Instance Method Details

#retry_on_error(errors:, limit: 10, on_error: nil, &block) ⇒ Object

Calls the supplied block, traps the given types of errors retrying up to a given number of times

Parameters:

  • errors (Array<Exception>)

    the exceptions to trap

  • limit (Integer) (defaults to: 10)

    the maximum number of retries

  • on_error (Proc) (defaults to: nil)

    a block to call when an error occurs

Returns:

  • the result of any successful completion of the block

Raises:

  • any error ocurring more than ‘limit` times



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/imap/backup/retry_on_error.rb', line 16

def retry_on_error(errors:, limit: 10, on_error: nil, &block)
  tries ||= 1
  block.call
rescue *errors => e
  if tries < limit
    message = "#{e}, attempt #{tries} of #{limit}"
    Logger.logger.debug message
    on_error&.call
    tries += 1
    retry
  end
  raise e
end