Sha256: 828c7b891ebf2b59b51cc06c47bdb7d9b21cf6053abcf2cfae7adb2810d79afc

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

module Fluent
  module BigQuery
    # @abstract
    class Error < StandardError
      RETRYABLE_ERROR_REASON = %w(backendError internalError rateLimitExceeded tableUnavailable).freeze
      RETRYABLE_STATUS_CODE = [500, 503]

      class << self
        def wrap(google_api_error, message = nil, force_unretryable: false)
          e = google_api_error
          return UnRetryableError.new(message, e) if force_unretryable

          if retryable_error?(e)
            RetryableError.new(message, e)
          else
            UnRetryableError.new(message, e)
          end
        end

        def retryable_error?(google_api_error)
          e = google_api_error
          reason = e.respond_to?(:reason) ? e.reason : nil

          retryable_error_reason?(reason) ||
            (e.is_a?(Google::Apis::ServerError) && RETRYABLE_STATUS_CODE.include?(e.status_code))
        end

        def retryable_error_reason?(reason)
          RETRYABLE_ERROR_REASON.include?(reason)
        end

        # Guard for instantiation
        private :new
        def inherited(subclass)
          subclass.class_eval do
            class << self
              public :new
            end
          end
        end
      end

      attr_reader :origin

      def initialize(message, origin = nil)
        @origin = origin
        super(message || origin.message)
      end

      def method_missing(name, *args)
        if @origin
          @origin.send(name, *args)
        else
          super
        end
      end

      def reason
        @origin && @origin.respond_to?(:reason) ? @origin.reason : nil
      end

      def status_code
        @origin && @origin.respond_to?(:status_code) ? @origin.status_code : nil
      end

      def body
        @origin && @origin.respond_to?(:body) ? @origin.body : nil
      end

      def retryable?
        false
      end
    end

    class UnRetryableError < Error; end

    class RetryableError < Error
      def retryable?
        true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fluent-plugin-bigquery-0.4.0 lib/fluent/plugin/bigquery/errors.rb
fluent-plugin-bigquery-0.3.4 lib/fluent/plugin/bigquery/errors.rb
fluent-plugin-bigquery-0.3.3 lib/fluent/plugin/bigquery/errors.rb