Exception: Koala::Facebook::APIError

Inherits:
KoalaError
  • Object
show all
Defined in:
lib/koala/errors.rb

Overview

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_status, response_body, error_info = nil) ⇒ Object

Create a new API Error

Parameters:

  • http_status (Integer)

    The HTTP status code of the response

  • response_body (String)

    The response body

  • error_info (defaults to: nil)

    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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/koala/errors.rb', line 44

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.parse(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["fbtrace_id"]

    self.fb_error_debug_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"]
    self.fb_buc_usage = json_parse_for(error_info, "x-business-use-case-usage")
    self.fb_ada_usage = json_parse_for(error_info, "x-ad-account-usage")
    self.fb_app_usage = json_parse_for(error_info, "x-app-usage")

    error_array = []
    %w(type code error_subcode message error_user_title error_user_msg fbtrace_id x-fb-trace-id x-fb-debug x-fb-rev).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

Instance Attribute Details

#fb_ada_usageObject

Returns the value of attribute fb_ada_usage.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_ada_usage
  @fb_ada_usage
end

#fb_app_usageObject

Returns the value of attribute fb_app_usage.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_app_usage
  @fb_app_usage
end

#fb_buc_usageObject

Returns the value of attribute fb_buc_usage.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_buc_usage
  @fb_buc_usage
end

#fb_error_codeObject

Returns the value of attribute fb_error_code.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_code
  @fb_error_code
end

#fb_error_debugObject

Returns the value of attribute fb_error_debug.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_debug
  @fb_error_debug
end

#fb_error_debug_trace_idObject

Returns the value of attribute fb_error_debug_trace_id.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_debug_trace_id
  @fb_error_debug_trace_id
end

#fb_error_messageObject

Returns the value of attribute fb_error_message.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_message
  @fb_error_message
end

#fb_error_revObject

Returns the value of attribute fb_error_rev.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_rev
  @fb_error_rev
end

#fb_error_subcodeObject

Returns the value of attribute fb_error_subcode.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_subcode
  @fb_error_subcode
end

#fb_error_trace_idObject

Returns the value of attribute fb_error_trace_id.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_trace_id
  @fb_error_trace_id
end

#fb_error_typeObject

Returns the value of attribute fb_error_type.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_type
  @fb_error_type
end

#fb_error_user_msgObject

Returns the value of attribute fb_error_user_msg.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_user_msg
  @fb_error_user_msg
end

#fb_error_user_titleObject

Returns the value of attribute fb_error_user_title.



16
17
18
# File 'lib/koala/errors.rb', line 16

def fb_error_user_title
  @fb_error_user_title
end

#http_statusObject

Returns the value of attribute http_status.



16
17
18
# File 'lib/koala/errors.rb', line 16

def http_status
  @http_status
end

#response_bodyObject

Returns the value of attribute response_body.



16
17
18
# File 'lib/koala/errors.rb', line 16

def response_body
  @response_body
end