module TwitterRetry::Retryable

Public Instance Methods

ignorable?(error) click to toggle source

whether ignorable error @param error [Exception]

# File lib/twitter_retry/retryable.rb, line 38
def ignorable?(error)
  match_any_error?(error, TwitterRetry.config.ignorable_errors)
end
retryable?(error) click to toggle source

whether retryable error @param error [Exception]

# File lib/twitter_retry/retryable.rb, line 32
def retryable?(error)
  match_any_error?(error, TwitterRetry.config.retryable_errors)
end
suspended?(error) click to toggle source

whether suspended user error

# File lib/twitter_retry/retryable.rb, line 43
def suspended?(error)
  error.is_a?(Twitter::Error::Forbidden) &&
    error.message.include?("Your account is suspended and is not permitted to access this feature.")
end
with_handing() { || ... } click to toggle source

retry when error occurred matched with {#RETRYABLE_ERRORS} @return [true] successful @return [false] error is ignored (ex. {Twitter::Error::Forbidden}) @raise [SuspendedError] @raise [RetryOverError] retried {TwitterRetry::TwitterRetry#max_retry_count} count, but all failure @raise [CannotRetryableError]

# File lib/twitter_retry/retryable.rb, line 9
def with_handing
  retry_count = 0

  begin
    yield

  rescue => error
    return false               if ignorable?(error)
    raise SuspendedError       if suspended?(error)
    raise CannotRetryableError unless retryable?(error)
    raise RetryOverError       unless retry_count < TwitterRetry.config.max_retry_count

    retry_count += 1
    sleep(TwitterRetry.config.sleep_second)
    retry
  end

  # successful
  true
end

Private Instance Methods

match_any_error?(source_error, check_errors) click to toggle source
# File lib/twitter_retry/retryable.rb, line 50
def match_any_error?(source_error, check_errors)
  check_errors.any? do |error_class, message|
    match_error?(source_error, error_class, message)
  end
end
match_error?(source_error, error_class, message) click to toggle source
# File lib/twitter_retry/retryable.rb, line 56
def match_error?(source_error, error_class, message)
  return false unless source_error.is_a?(error_class)

  # Check only error class
  return true unless message

  if message.is_a?(Regexp)
    source_error.message =~ message
  else
    source_error.message.include?(message)
  end
end