Exception: WhatsAppCloudApi::Errors::GraphApiError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/whatsapp_cloud_api/errors.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, http_status:, code: nil, type: nil, details: nil, error_subcode: nil, fbtrace_id: nil, error_data: nil, category: nil, retry_hint: nil, raw_response: nil, retry_after: nil) ⇒ GraphApiError

Returns a new instance of GraphApiError.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/whatsapp_cloud_api/errors.rb', line 90

def initialize(message: nil, http_status:, code: nil, type: nil, details: nil, 
               error_subcode: nil, fbtrace_id: nil, error_data: nil, 
               category: nil, retry_hint: nil, raw_response: nil, retry_after: nil)
  @http_status = http_status
  @code = code || http_status
  @type = type || 'GraphApiError'
  @details = details
  @error_subcode = error_subcode
  @fbtrace_id = fbtrace_id
  @error_data = error_data
  @retry_after = retry_after
  @category = category || categorize_error_code(@code, @http_status)
  @retry_hint = retry_hint || derive_retry_hint
  @raw_response = raw_response

  error_message = message || build_error_message
  super(error_message)
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def category
  @category
end

#codeObject (readonly)

Returns the value of attribute code.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def code
  @code
end

#detailsObject (readonly)

Returns the value of attribute details.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def details
  @details
end

#error_dataObject (readonly)

Returns the value of attribute error_data.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def error_data
  @error_data
end

#error_subcodeObject (readonly)

Returns the value of attribute error_subcode.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def error_subcode
  @error_subcode
end

#fbtrace_idObject (readonly)

Returns the value of attribute fbtrace_id.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def fbtrace_id
  @fbtrace_id
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def http_status
  @http_status
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def raw_response
  @raw_response
end

#retry_afterObject (readonly)

Returns the value of attribute retry_after.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def retry_after
  @retry_after
end

#retry_hintObject (readonly)

Returns the value of attribute retry_hint.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def retry_hint
  @retry_hint
end

#typeObject (readonly)

Returns the value of attribute type.



87
88
89
# File 'lib/whatsapp_cloud_api/errors.rb', line 87

def type
  @type
end

Class Method Details

.from_response(response, body = nil, raw_text = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/whatsapp_cloud_api/errors.rb', line 110

def from_response(response, body = nil, raw_text = nil)
  http_status = response.status
  retry_after_ms = parse_retry_after(response.headers['retry-after'])

  # Ensure body is a hash for processing
  unless body.is_a?(Hash)
    if body.is_a?(String)
      begin
        body = JSON.parse(body)
      rescue JSON::ParserError
        body = {}
      end
    else
      body = {}
    end
  end

  # Check for Graph API error envelope
  if body.key?('error')
    error_payload = body['error']
    code = error_payload['code'] || http_status
    type = error_payload['type'] || 'GraphApiError'
    details = error_payload.is_a?(Hash) ? error_payload.dig('error_data', 'details') : nil
    
    new(
      message: error_payload['message'],
      http_status: http_status,
      code: code,
      type: type,
      details: details,
      error_subcode: error_payload['error_subcode'],
      fbtrace_id: error_payload['fbtrace_id'],
      error_data: error_payload['error_data'],
      retry_hint: build_retry_hint_with_delay(code, http_status, retry_after_ms),
      raw_response: body
    )
  elsif body.is_a?(Hash) && body.key?('error') && body['error'].is_a?(String)
    # Kapso proxy error format
    category = http_status >= 500 ? :server : categorize_error_code(nil, http_status)
    new(
      message: body['error'],
      http_status: http_status,
      code: http_status,
      category: category,
      retry_hint: build_retry_hint_with_delay(http_status, http_status, retry_after_ms),
      raw_response: body
    )
  else
    # Generic HTTP error
    category = http_status >= 500 ? :server : categorize_error_code(nil, http_status)
    message = build_default_message(http_status, nil, raw_text)
    
    new(
      message: message,
      http_status: http_status,
      code: http_status,
      category: category,
      retry_hint: build_retry_hint_with_delay(http_status, http_status, retry_after_ms),
      raw_response: raw_text || body
    )
  end
end

Instance Method Details

#auth_error?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/whatsapp_cloud_api/errors.rb', line 242

def auth_error?
  category == :authorization
end

#rate_limit?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/whatsapp_cloud_api/errors.rb', line 246

def rate_limit?
  category == :throttling
end

#requires_token_refresh?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/whatsapp_cloud_api/errors.rb', line 260

def requires_token_refresh?
  category == :authorization || REFRESH_TOKEN_CODES.include?(code)
end

#retryable?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/whatsapp_cloud_api/errors.rb', line 264

def retryable?
  ![:do_not_retry].include?(retry_hint[:action])
end

#template_error?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/whatsapp_cloud_api/errors.rb', line 256

def template_error?
  category == :template
end

#temporary?Boolean

Returns:

  • (Boolean)


250
251
252
253
254
# File 'lib/whatsapp_cloud_api/errors.rb', line 250

def temporary?
  [:throttling, :server, :synchronization].include?(category) || 
  http_status >= 500 || 
  [1, 2, 17, 341].include?(code)
end

#to_hObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/whatsapp_cloud_api/errors.rb', line 268

def to_h
  {
    name: self.class.name,
    message: message,
    http_status: http_status,
    code: code,
    type: type,
    details: details,
    error_subcode: error_subcode,
    fbtrace_id: fbtrace_id,
    category: category,
    retry_hint: retry_hint,
    raw_response: raw_response
  }
end