Sha256: e044bdc173a154fbe5262854437014f7e38fffe06b3802cd51a194eebcd7257c

Contents?: true

Size: 1.38 KB

Versions: 19

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module MyApiClient
  # Description of Exceptions
  module Exceptions
    extend ActiveSupport::Concern
    include ActiveSupport::Rescuable

    # Description of #call
    #
    # @param args [Array<Object>] describe_args_here
    # @return [Object] description_of_returned_object
    def call(*args)
      @args = args
      send(*args)
    rescue StandardError => e
      @retry_count ||= 0
      raise unless rescue_with_handler(e)

      retry_result
    end

    private

    attr_reader :retry_count, :retry_result, :method_name, :args

    class_methods do
      def retry_on(*exception, wait: 1.second, attempts: 3)
        rescue_from(*exception) do |error|
          if retry_count < attempts
            retry_calling(wait)
          elsif block_given?
            yield self, error
          else
            raise error
          end
        end
      end

      # Description of #discard_on
      #
      # @note
      #   !! It is implemented following ActiveJob, but I think this method is
      #   not useful in this gem. !!
      # @param exception [Type] describe_exception_here
      def discard_on(*exception)
        rescue_from(*exception) do |error|
          yield self, error if block_given?
        end
      end
    end

    def retry_calling(wait)
      Sleeper.call(wait: wait)
      @retry_count += 1
      @retry_result = call(*args)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
my_api_client-1.0.0 lib/my_api_client/exceptions.rb
my_api_client-0.27.0 lib/my_api_client/exceptions.rb
my_api_client-0.26.0 lib/my_api_client/exceptions.rb
my_api_client-0.25.0 lib/my_api_client/exceptions.rb
my_api_client-0.24.0 lib/my_api_client/exceptions.rb
my_api_client-0.23.0 lib/my_api_client/exceptions.rb
my_api_client-0.22.0 lib/my_api_client/exceptions.rb
my_api_client-0.21.0 lib/my_api_client/exceptions.rb
my_api_client-0.20.0 lib/my_api_client/exceptions.rb
my_api_client-0.19.0 lib/my_api_client/exceptions.rb
my_api_client-0.18.0 lib/my_api_client/exceptions.rb
my_api_client-0.17.0 lib/my_api_client/exceptions.rb
my_api_client-0.16.1 lib/my_api_client/exceptions.rb
my_api_client-0.16.0 lib/my_api_client/exceptions.rb
my_api_client-0.15.0 lib/my_api_client/exceptions.rb
my_api_client-0.14.0 lib/my_api_client/exceptions.rb
my_api_client-0.14.0.pre lib/my_api_client/exceptions.rb
my_api_client-0.13.0 lib/my_api_client/exceptions.rb
my_api_client-0.12.0 lib/my_api_client/exceptions.rb