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)
Facebook has a set of standardized error codes, some of which represent problems with the token.
Facebook can return debug information in the response headers – see developers.facebook.com/docs/graph-api/using-graph-api#bugdebug
# File lib/koala/api/graph_error_checker.rb, line 22 def error_if_appropriate if http_status >= 400 error_class.new(http_status, body, error_info) end end
# File lib/koala/api/graph_error_checker.rb, line 40 def auth_error? # tbh, I'm not sure why we restrict Facebook-reported OAuthExceptions to only those without # codes or whose codes match the list above -- let's investigate changing this later. error_info['type'] == 'OAuthException' && (!error_info['code'] || AUTHENTICATION_ERROR_CODES.include?(error_info['code'].to_i)) end
# File lib/koala/api/graph_error_checker.rb, line 56 def base_error_info response_hash['error'] || {} end
# File lib/koala/api/graph_error_checker.rb, line 30 def error_class if auth_error? # See: https://developers.facebook.com/docs/authentication/access-token-expiration/ # https://developers.facebook.com/bugs/319643234746794?browse=search_4fa075c0bd9117b20604672 AuthenticationError else ClientError end end
# File lib/koala/api/graph_error_checker.rb, line 47 def error_info # Build up the complete error info from whatever Facebook gives us plus the header # information @error_info ||= DEBUG_HEADERS.inject(base_error_info) do |hash, error_key| hash[error_key] = headers[error_key] if headers[error_key] hash end end
# File lib/koala/api/graph_error_checker.rb, line 60 def response_hash # Normally, we start with the response body. If it isn't valid JSON, we start with an empty # hash and fill it with error data. @response_hash ||= begin JSON.load(body) rescue JSON::ParserError {} end end
# File lib/koala/api/graph_error_checker.rb, line 8 def initialize(http_status, body, headers) @http_status = http_status.to_i @body = body @headers = headers end