Exception: FbGraph::Exception

Inherits:
StandardError
  • Object
show all
Defined in:
lib/fb_graph/exception.rb

Constant Summary collapse

ERROR_HEADER_MATCHERS =
{
  /invalid_token/ => "InvalidToken",
  /invalid_request/ => "InvalidRequest"
}
ERROR_EXCEPTION_MATCHERS =
{
  /Could\snot\ssave\screative/          => "CreativeNotSaved",
  /QueryLockTimeoutException/           => "QueryLockTimeout",
  /Could\snot\screate\stargeting\sspec/ => "TargetingSpecNotSaved",
  /Could\snot\sfetch\sadgroups/         => "AdgroupFetchFailure",
  /Failed\sto\sopen\sprocess/           => "OpenProcessFailure",
  /Could\snot\scommit\stransaction/     => "TransactionCommitFailure",
  /QueryErrorException/                 => "QueryError",
  /QueryConnectionException/            => "QueryConnection",
  /QueryDuplicateKeyException/          => "QueryDuplicateKey"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, message, body = '') ⇒ Exception

Returns a new instance of Exception.



63
64
65
66
67
68
69
70
71
# File 'lib/fb_graph/exception.rb', line 63

def initialize(code, message, body = '')
  @code = code
  if body.present?
    response = JSON.parse(body).with_indifferent_access
    message = response[:error][:message]
    @type = response[:error][:type]
  end
  super message
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



3
4
5
# File 'lib/fb_graph/exception.rb', line 3

def code
  @code
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/fb_graph/exception.rb', line 3

def type
  @type
end

Class Method Details

.handle_httpclient_error(response, headers) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fb_graph/exception.rb', line 22

def self.handle_httpclient_error(response, headers)
  return nil unless response[:error]

  # Check the WWW-Authenticate header, since it seems to be more standardized than the response
  # body error information.
  # The complex lookup is needed to follow the HTTP spec - headers should be looked up
  # without regard to case.
  www_authenticate = headers.select do |name, value|
    name.upcase == "WWW-Authenticate".upcase
  end.to_a.flatten.second
  if www_authenticate
    # Session expiration/invalidation is very common. Check for that first.
    if www_authenticate =~ /invalid_token/ && response[:error][:message] =~ /session has been invalidated/
      raise InvalidSession.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    end

    ERROR_HEADER_MATCHERS.keys.each do |matcher|
      if matcher =~ www_authenticate
        exception_class = FbGraph::const_get(ERROR_HEADER_MATCHERS[matcher])
        raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
      end
    end
  end

  # If we can't match on WWW-Authenticate, use the type
  case response[:error][:type]
  when /OAuth/
    raise Unauthorized.new("#{response[:error][:type]} :: #{response[:error][:message]}")
  else
    exception_class = nil
    ERROR_EXCEPTION_MATCHERS.keys.each do |matcher|
      exception_class = FbGraph::const_get(ERROR_EXCEPTION_MATCHERS[matcher]) if matcher =~ response[:error][:message]
    end
    if exception_class
      raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    else
      raise BadRequest.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    end
  end
end