Class: Google::Ads::GoogleAds::Interceptors::ErrorInterceptor

Inherits:
GRPC::ClientInterceptor
  • Object
show all
Defined in:
lib/google/ads/google_ads/interceptors/error_interceptor.rb

Constant Summary collapse

ERROR_TRANSFORMER =
lambda do |gax_error|
  begin
    gax_error.status_details.each do |detail|
      # If there is an underlying GoogleAdsFailure, throw that one.
      if detail.class.name.start_with?("Google::Ads::GoogleAds") &&
          detail.class.name.end_with?("GoogleAdsFailure")
        return Google::Ads::GoogleAds::Errors::GoogleAdsError.new(
          detail
        )
      elsif detail.is_a?(Google::Protobuf::Any)
        type = Google::Protobuf::DescriptorPool.generated_pool.lookup(
          detail.type_name
        ).msgclass
        failure = detail.unpack(type)

        return Google::Ads::GoogleAds::Errors::GoogleAdsError.new(
          failure
        )
      end
    end
  rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e
    return e
  rescue NoMethodError
    # Sometimes status_details is just a String; in that case, we should
    # just raise the original exception.
  end
  # If we don't find an error of the correct type, or if we run into an
  # error while processing, just throw the original.
  gax_error
end

Instance Method Summary collapse

Constructor Details

#initializeErrorInterceptor

Returns a new instance of ErrorInterceptor.



57
58
59
60
61
# File 'lib/google/ads/google_ads/interceptors/error_interceptor.rb', line 57

def initialize()
  # Don't propagate args, parens are necessary
  super()
  @error_transformer = ERROR_TRANSFORMER
end

Instance Method Details

#request_response(request:, call:, method:, metadata: {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/google/ads/google_ads/interceptors/error_interceptor.rb', line 63

def request_response(request:, call:, method:, metadata: {})
  begin
    yield
  rescue Exception => e
    raise @error_transformer.call(e)
  end
end

#server_streamer(request:, call:, method:, metadata: {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/google/ads/google_ads/interceptors/error_interceptor.rb', line 71

def server_streamer(request:, call:, method:, metadata: {})
  responses = yield
  Enumerator.new do |y|
    responses.each { |response| y << response }
  rescue BasicObject => e
    raise @error_transformer.call(e)
  end
end