class Koala::Facebook::APIError

Facebook responded with an error to an API request. If the exception contains a nil #http_status, then the error was detected before making a call to Facebook. (e.g. missing access token)

Attributes

fb_error_code[RW]
fb_error_debug[RW]
fb_error_message[RW]
fb_error_rev[RW]
fb_error_subcode[RW]
fb_error_trace_id[RW]
fb_error_type[RW]
fb_error_user_msg[RW]
fb_error_user_title[RW]
http_status[RW]
response_body[RW]

Public Class Methods

new(http_status, response_body, error_info = nil) click to toggle source

Create a new API Error

@param #http_status [Integer] The HTTP status code of the response @param #response_body [String] The response body @param error_info One of the following:

[Hash] The error information extracted from the request
       ("type", "code", "error_subcode", "message")
[String] The error description
If error_info is nil or not provided, the method will attempt to extract
the error info from the response_body

@return the newly created APIError

# File lib/koala/errors.rb, line 40
def initialize(http_status, response_body, error_info = nil)
  if response_body
    self.response_body = response_body.strip
  else
    self.response_body = ''
  end
  self.http_status = http_status

  if error_info && error_info.is_a?(String)
    message = error_info
  else
    unless error_info
      begin
        error_info = JSON.load(response_body)['error'] if response_body
      rescue
      end
      error_info ||= {}
    end

    self.fb_error_type = error_info["type"]
    self.fb_error_code = error_info["code"]
    self.fb_error_subcode = error_info["error_subcode"]
    self.fb_error_message = error_info["message"]
    self.fb_error_user_msg = error_info["error_user_msg"]
    self.fb_error_user_title = error_info["error_user_title"]

    self.fb_error_trace_id = error_info["x-fb-trace-id"]
    self.fb_error_debug = error_info["x-fb-debug"]
    self.fb_error_rev = error_info["x-fb-rev"]

    error_array = []
    %w(type code error_subcode message error_user_title error_user_msg x-fb-trace-id).each do |key|
      error_array << "#{key}: #{error_info[key]}" if error_info[key]
    end

    if error_array.empty?
      message = self.response_body
    else
      message = error_array.join(', ')
    end
  end
  message += " [HTTP #{http_status}]" if http_status

  super(message)
end