lib/google/ads/google_ads/errors.rb in google-ads-googleads-1.1.1 vs lib/google/ads/google_ads/errors.rb in google-ads-googleads-2.0.0
- old
+ new
@@ -32,9 +32,68 @@
def initialize(failure)
@failure = failure
end
end
+
+ def self.namespaces
+ ERROR_NAMESPACES.values
+ end
+
+ # takes a Google::Ads::GoogleAds::VX::Errors::GoogleAdsError error
+ # and extracts its index in the list of mutations
+ #
+ # Return nil if not found
+ # Return Integer: index of the error
+ def self.index(error)
+ path = error.location.field_path_elements.find {|elt| elt.field_name == OPERATIONS }
+ if path
+ path.index.value
+ else
+ nil
+ end
+ end
+
+ # takes a Google::Ads::GoogleAds::VX::Errors::GoogleAdsError error
+ # and extracts a full error message based on the message and the different paths
+ # describing the source of the error
+ #
+ # Returns a string describing the error
+ def self.message(error)
+ error.location.field_path_elements.inject(error.message) do |string, path|
+ unless MEANINGLESS_PATHS.include?(path.field_name)
+ string = "#{path.field_name} - #{string}"
+ end
+ string
+ end
+ end
+
+ # takes a Google::Ads::GoogleAds::VX::Errors::GoogleAdsError error
+ # and extracts error code in the form of a hash
+ #
+ # Returns a Hash { name:, value:}
+ def self.code(error, version = Google::Ads::GoogleAds.default_api_version)
+ mapping = ERROR_CODES_MAPPING.fetch(version)
+ match = mapping.find do |error_name|
+ error.error_code.send(error_name) != :UNSPECIFIED
+ end
+ if match
+ { name: match, value: error.error_code.send(match) }
+ else
+ { }
+ end
+ end
+
+ ERROR_NAMESPACES = Google::Ads::GoogleAds.known_api_versions.each_with_object({}) do |v, h|
+ require "google/ads/google_ads/#{v.downcase}/errors/errors_pb"
+ h[v] = const_get("Google::Ads::GoogleAds::#{v}::Errors")
+ end
+
+ ERROR_CODES_MAPPING = ERROR_NAMESPACES.each_with_object({}) do |(version, klass), hash|
+ hash[version] = klass.const_get(:ErrorCode).descriptor.lookup_oneof('error_code').map(&:name)
+ end
+ MEANINGLESS_PATHS = %w(operations create update remove)
+ OPERATIONS = "operations"
end
end
end
end