Sha256: 905ec628ad3955b3d161f01afea792f2aef2167fcfa609a8564f245ebf4d01a7

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

module TwitterRetry
  module Retryable
    # 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]
    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

    # whether retryable error
    # @param error [Exception]
    def retryable?(error)
      match_any_error?(error, TwitterRetry.config.retryable_errors)
    end

    # whether ignorable error
    # @param error [Exception]
    def ignorable?(error)
      match_any_error?(error, TwitterRetry.config.ignorable_errors)
    end

    # whether suspended user error
    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

    private

    def match_any_error?(source_error, check_errors)
      check_errors.any? do |error_class, message|
        match_error?(source_error, error_class, message)
      end
    end

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twitter_retry-0.2.2 lib/twitter_retry/retryable.rb
twitter_retry-0.2.1 lib/twitter_retry/retryable.rb
twitter_retry-0.2.0 lib/twitter_retry/retryable.rb