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.



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

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

Instance Attribute Details

#codeObject Also known as: error_code

Returns the value of attribute code.



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

def code
  @code
end

#error_subcodeObject

Returns the value of attribute error_subcode.



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

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



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

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



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

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



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

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