Exception: FbGraph2::Exception

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

Defined Under Namespace

Classes: BadRequest, InternalServerError, InvalidRequest, InvalidToken, NotFound, Unauthorized

Constant Summary collapse

ERROR_HEADER_MATCHERS =
{
  /not_found/       => NotFound,
  /invalid_token/   => InvalidToken,
  /invalid_request/ => InvalidRequest
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, message, error = {}) ⇒ Exception

Returns a new instance of Exception.



41
42
43
44
45
46
# File 'lib/fb_graph2/exception.rb', line 41

def initialize(status, message, error = {})
  super message
  self.status = status
  self.type = error[:type]
  self.code = error[:code]
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.detect(status, body = {}, headers = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fb_graph2/exception.rb', line 6

def detect(status, body = {}, headers = {})
  error = body[:error]
  message = error.try(:[], :message)
  klass = detect_from_header(headers, error) || detect_from_status(status)
  if klass
    klass.new message, error
  else
    new status, message, error
  end
end

.detect_from_header(headers, error) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/fb_graph2/exception.rb', line 30

def detect_from_header(headers, error)
  key, value = headers.detect do |name, value|
    name.upcase == "WWW-Authenticate".upcase
  end || return
  matched, klass = ERROR_HEADER_MATCHERS.detect do |matcher, klass_name|
    matcher =~ value
  end || return
  klass
end

.detect_from_status(status) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fb_graph2/exception.rb', line 17

def detect_from_status(status)
  case status
  when 400
    BadRequest
  when 401
    Unauthorized
  when 404
    NotFound
  when 500
    InternalServerError
  end
end