Module: Google::Ads::GoogleAds::Errors

Defined in:
lib/google/ads/google_ads/errors.rb

Defined Under Namespace

Classes: Error, GoogleAdsError

Constant Summary collapse

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"

Class Method Summary collapse

Class Method Details

.code(error, version = nil) ⇒ Object

takes a Google::Ads::GoogleAds::VX::Errors::GoogleAdsError error and extracts error code in the form of a hash

Returns a Hash { name:, value:}



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/google/ads/google_ads/errors.rb', line 84

def self.code(error, version = nil)
  error_version = error.class.name.split("::")[3]
  if error_version.nil?
    raise RuntimeError, "passed error is not a google ads class"
  end

  error_version = error_version.upcase.to_sym

  if version != nil
    Deprecation.new(false, false).deprecate(
      "Passing explicit versions to #code is deprecated, instead" \
      " we now infer it from the passed object."
    )
  end

  if version != nil && error_version.to_s != version.to_s
    raise ArgumentError,
      "passed version must match verison class of error"
  end

  version = error_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

.index(error) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/google/ads/google_ads/errors.rb', line 51

def self.index(error)
  path = error.location.field_path_elements.find {|elt| elt.field_name == OPERATIONS }
  if path
    ret = path.index
    # TODO: Once v5 is sunset, we can always just return path.index.
    if ret.class == Integer
      ret
    else
      ret.value
    end
  else
    nil
  end
end

.message(error) ⇒ Object

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



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

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

.namespacesObject



42
43
44
# File 'lib/google/ads/google_ads/errors.rb', line 42

def self.namespaces
  ERROR_NAMESPACES.values
end