class Koala::Facebook::GraphErrorChecker

This class, given a Koala::HTTPService::Response object, will check for Graph API-specific errors. This returns an error of the appropriate type which can be immediately raised (non-batch) or added to the list of batch results (batch)

Constants

AUTHENTICATION_ERROR_CODES

Facebook has a set of standardized error codes, some of which represent problems with the token.

DEBUG_HEADERS

Facebook can return debug information in the response headers – see developers.facebook.com/docs/graph-api/using-graph-api#bugdebug

Attributes

body[R]
headers[R]
http_status[R]

Public Class Methods

new(http_status, body, headers) click to toggle source
   # File lib/koala/api/graph_error_checker.rb
 8 def initialize(http_status, body, headers)
 9   @http_status = http_status.to_i
10   @body = body
11   @headers = headers
12 end

Public Instance Methods

error_if_appropriate() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
22 def error_if_appropriate
23   if http_status >= 400
24     error_class.new(http_status, body, error_info)
25   end
26 end

Protected Instance Methods

auth_error?() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
40 def auth_error?
41   # tbh, I'm not sure why we restrict Facebook-reported OAuthExceptions to only those without
42   # codes or whose codes match the list above -- let's investigate changing this later.
43   error_info['type'] == 'OAuthException' &&
44     (!error_info['code'] || AUTHENTICATION_ERROR_CODES.include?(error_info['code'].to_i))
45 end
base_error_info() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
56 def base_error_info
57   response_hash['error'] || {}
58 end
error_class() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
30 def error_class
31   if auth_error?
32     # See: https://developers.facebook.com/docs/authentication/access-token-expiration/
33     #      https://developers.facebook.com/bugs/319643234746794?browse=search_4fa075c0bd9117b20604672
34     AuthenticationError
35   else
36     ClientError
37   end
38 end
error_info() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
47 def error_info
48   # Build up the complete error info from whatever Facebook gives us plus the header
49   # information
50   @error_info ||= DEBUG_HEADERS.inject(base_error_info) do |hash, error_key|
51     hash[error_key] = headers[error_key] if headers[error_key]
52     hash
53   end
54 end
response_hash() click to toggle source
   # File lib/koala/api/graph_error_checker.rb
60 def response_hash
61   # Normally, we start with the response body. If it isn't valid JSON, we start with an empty
62   # hash and fill it with error data.
63   @response_hash ||= begin
64     JSON.parse(body)
65   rescue JSON::ParserError
66     {}
67   end
68 end