Exception: FbGraph::Exception

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Exception.



72
73
74
75
76
77
78
79
80
# File 'lib/fb_graph/exception.rb', line 72

def initialize(status, message, details = {})
  @status = status
  if (error = details.try(:[], :error))
    @type          = error[:type]
    @error_code    = error[:code]
    @error_subcode = error[:error_subcode]
  end
  super message
end

Instance Attribute Details

#error_codeObject

Returns the value of attribute error_code.



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

def error_code
  @error_code
end

#error_subcodeObject

Returns the value of attribute error_subcode.



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

def error_subcode
  @error_subcode
end

#statusObject Also known as: code

Returns the value of attribute status.



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

def status
  @status
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_response(status, message, details = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/fb_graph/exception.rb', line 22

def handle_response(status, message, details = {})
  klass = klass_for_status status
  exception = if klass
    klass.new message, details
  else
    Exception.new status, message, details
  end
  raise exception
end

.handle_structured_response(status, details, headers) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fb_graph/exception.rb', line 7

def handle_structured_response(status, details, headers)
  if (error = details[:error])
    klass = klass_for_header(headers, error) || klass_for_structured_body(error)
    message = [error[:type], error[:message]].join(' :: ')
    if klass
      raise klass.new(message, details)
    else
      handle_response status, message, details
    end
  else
    message = [details[:error_code], details[:error_msg]].compact.join(' :: ')
    handle_response status, message, details
  end
end

.klass_for_header(headers, error) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fb_graph/exception.rb', line 45

def klass_for_header(headers, error)
  key, value = headers.detect do |name, value|
    name.upcase == "WWW-Authenticate".upcase
  end || return
  if value =~ /invalid_token/ && error[:message] =~ /session has been invalidated/
    InvalidSession
  else
    matched, klass = ERROR_HEADER_MATCHERS.detect do |matcher, klass_name|
      matcher =~ value
    end || return
    klass
  end
end

.klass_for_status(status) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fb_graph/exception.rb', line 32

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

.klass_for_structured_body(error) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fb_graph/exception.rb', line 59

def klass_for_structured_body(error)
  case error[:type]
  when /OAuth/
    Unauthorized
  else
    matched, klass = ERROR_EXCEPTION_MATCHERS.detect do |matcher, klass_name|
      matcher =~ error[:message]
    end || return
    klass
  end
end