Exception: FbGraph::Exception

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

Direct Known Subclasses

BadRequest, NotFound, Unauthorized

Constant Summary collapse

ERROR_HEADER_MATCHERS =
{
  /invalid_token/ => "InvalidToken",
  /invalid_request/ => "InvalidRequest"
}

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.



38
39
40
41
42
43
44
45
46
47
# File 'lib/fb_graph/exception.rb', line 38

def initialize(code, message, body = '')
  @code = code
  if body.blank?
    @message = message
  else
    response = JSON.parse(body).with_indifferent_access
    @message = response[:error][:message]
    @type = response[:error][:type]
  end
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

#messageObject

Returns the value of attribute message.



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

def message
  @message
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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fb_graph/exception.rb', line 10

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.
  if www_authenticate = headers["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
    raise BadRequest.new("#{response[:error][:type]} :: #{response[:error][:message]}")
  end
end