fb_graph 2.7.3 → 2.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c41a5db1d62f6356aceae001c8aa779c785a6f5e
4
- data.tar.gz: d08ed080c63f5278124d1001e52ca480abac4cd7
3
+ metadata.gz: f394a8d8b3ac2e339d04d51b5cd3cb8f84326acd
4
+ data.tar.gz: 3ec54684dd935ae3f7f86c2cb7a040e4ffb6c5d7
5
5
  SHA512:
6
- metadata.gz: 069da1d9cd6cae38ee5c189066c4ea17e3c3f1b02bf28b5f0012d3214046c3d271dac0234b4914b5377cda7a51ca58a5a0ad07de629368bc112d21324c83daf8
7
- data.tar.gz: 752610f699973e87cdea79e2c6dfb78c132f72524e11eefa52b25071ed840f95c34dd41a9f5cd1b85b3fa00bfdc5948930d54e6ba44eefcbef633e08b093006f
6
+ metadata.gz: 738a30bc38006244ac3aeeb59498e7a9565d4ad87004288ff344249df374e78f7f02ff54867c5b06012444cf60c049c29dbfc989cf029911fe9d43e83e181781
7
+ data.tar.gz: 347d5b4d682d68145126f151374c497836853c4261d65b9ff2be2ab90f29024cb75ab76712dd4d236dbaba02846f705a3cd64529de3cdbb3c3685c6672edbfd4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.7.3
1
+ 2.7.4
@@ -56,7 +56,7 @@ module FbGraph
56
56
  self.access_token = client.access_token! :client_auth_body
57
57
  self
58
58
  rescue Rack::OAuth2::Client::Error => e
59
- Exception.handle_rack_oauth2_error e
59
+ Exception.handle_response e.status, e.message
60
60
  end
61
61
 
62
62
  private
@@ -69,7 +69,7 @@ module FbGraph
69
69
  self.user = User.new(data[:user_id], :access_token => access_token)
70
70
  self
71
71
  rescue Rack::OAuth2::Client::Error => e
72
- Exception.handle_rack_oauth2_error e
72
+ Exception.handle_response e.status, e.message
73
73
  end
74
74
 
75
75
  def build_access_token(data)
@@ -1,6 +1,7 @@
1
1
  module FbGraph
2
2
  class Exception < StandardError
3
- attr_accessor :code, :type
3
+ attr_accessor :status, :type, :error_code, :error_subcode
4
+ alias_method :code, :status
4
5
 
5
6
  ERROR_HEADER_MATCHERS = {
6
7
  /not_found/ => "NotFound",
@@ -20,121 +21,123 @@ module FbGraph
20
21
  /QueryDuplicateKeyException/ => "QueryDuplicateKey"
21
22
  }
22
23
 
23
- def self.handle_httpclient_error(response, headers)
24
- return nil unless (response[:error] || response[:error_code] || response[:error_msg])
25
-
26
- # Sometimes we see an error that does not have the "error" field, but instead has both "error_code" and "error_msg"
27
- # example: {"error_code":1,"error_msg":"An unknown error occurred"}
28
- if (response[:error_code] && response[:error_msg] && !response[:error])
29
- raise InternalServerError.new("#{response[:error_code]} :: #{response[:error_msg]}")
24
+ class << self
25
+ def handle_structured_response(status, details, headers)
26
+ if (error = details[:error])
27
+ klass = klass_for_header(headers, error) || klass_for_structured_body(error)
28
+ message = [
29
+ error[:type] || 'unknown error type',
30
+ error[:message] || 'unknown error message'
31
+ ].join(' :: ')
32
+ if klass
33
+ raise klass.new(message, details)
34
+ else
35
+ handle_response status, message, details
36
+ end
37
+ else
38
+ message = [
39
+ details[:error_code] || 'unknown error_code',
40
+ details[:error_msg] || 'unknown error_msg'
41
+ ].compact.join(' :: ')
42
+ handle_response status, message, details
43
+ end
30
44
  end
31
45
 
32
- # Check the WWW-Authenticate header, since it seems to be more standardized than the response
33
- # body error information.
34
- # The complex lookup is needed to follow the HTTP spec - headers should be looked up
35
- # without regard to case.
36
- www_authenticate = headers.select do |name, value|
37
- name.upcase == "WWW-Authenticate".upcase
38
- end.to_a.flatten.second
39
- if www_authenticate
40
- # Session expiration/invalidation is very common. Check for that first.
41
- if www_authenticate =~ /invalid_token/ && response[:error][:message] =~ /session has been invalidated/
42
- raise InvalidSession.new("#{response[:error][:type]} :: #{response[:error][:message]}")
46
+ def handle_response(status, message, details = {})
47
+ klass = klass_for_status status
48
+ exception = if klass
49
+ klass.new message, details
50
+ else
51
+ Exception.new status, message, details
43
52
  end
53
+ raise exception
54
+ end
44
55
 
45
- ERROR_HEADER_MATCHERS.keys.each do |matcher|
46
- if matcher =~ www_authenticate
47
- exception_class = FbGraph::const_get(ERROR_HEADER_MATCHERS[matcher])
48
- raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
49
- end
56
+ def klass_for_status(status)
57
+ case status
58
+ when 400
59
+ BadRequest
60
+ when 401
61
+ Unauthorized
62
+ when 404
63
+ NotFound
64
+ when 500
65
+ InternalServerError
50
66
  end
51
67
  end
52
68
 
53
- # If we can't match on WWW-Authenticate, use the type
54
- case response[:error][:type]
55
- when /OAuth/
56
- raise Unauthorized.new("#{response[:error][:type]} :: #{response[:error][:message]}")
57
- else
58
- exception_class = nil
59
- ERROR_EXCEPTION_MATCHERS.keys.each do |matcher|
60
- exception_class = FbGraph::const_get(ERROR_EXCEPTION_MATCHERS[matcher]) if matcher =~ response[:error][:message]
61
- end
62
- if exception_class
63
- raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
69
+ def klass_for_header(headers, error)
70
+ key, value = headers.detect do |name, value|
71
+ name.upcase == "WWW-Authenticate".upcase
72
+ end || return
73
+ if value =~ /invalid_token/ && error[:message] =~ /session has been invalidated/
74
+ InvalidSession
64
75
  else
65
- raise BadRequest.new("#{response[:error][:type]} :: #{response[:error][:message]}")
76
+ matched, klass_name = ERROR_HEADER_MATCHERS.detect do |matcher, klass_name|
77
+ matcher =~ value
78
+ end || return
79
+ FbGraph::const_get klass_name
66
80
  end
67
81
  end
68
- end
69
82
 
70
- def self.handle_rack_oauth2_error(e)
71
- exception = case e.status
72
- when 400
73
- BadRequest.new(e.message)
74
- when 401
75
- Unauthorized.new(e.message)
76
- else
77
- Exception.new(e.status, e.message)
83
+ def klass_for_structured_body(error)
84
+ case error[:type]
85
+ when /OAuth/
86
+ Unauthorized
87
+ else
88
+ matched, klass_name = ERROR_EXCEPTION_MATCHERS.detect do |matcher, klass_name|
89
+ matcher =~ error[:message]
90
+ end || return
91
+ FbGraph::const_get klass_name
92
+ end
78
93
  end
79
- raise exception
80
94
  end
81
95
 
82
- def initialize(code, message, body = '')
83
- @code = code
84
- if body.present?
85
- response = MultiJson.load(body).with_indifferent_access
86
- message = response[:error][:message]
87
- @type = response[:error][:type]
96
+ def initialize(status, message, details = {})
97
+ @status = status
98
+ if (error = details.try(:[], :error))
99
+ @type = error[:type]
100
+ @error_code = error[:code]
101
+ @error_subcode = error[:error_subcode]
88
102
  end
89
103
  super message
90
104
  end
91
105
  end
92
106
 
93
107
  class BadRequest < Exception
94
- def initialize(message, body = '')
95
- super 400, message, body
108
+ def initialize(message, details = {})
109
+ super 400, message, details
96
110
  end
97
111
  end
98
112
 
99
113
  class Unauthorized < Exception
100
- def initialize(message, body = '')
101
- super 401, message, body
114
+ def initialize(message, details = {})
115
+ super 401, message, details
102
116
  end
103
117
  end
104
118
 
105
119
  class NotFound < Exception
106
- def initialize(message, body = '')
107
- super 404, message, body
120
+ def initialize(message, details = {})
121
+ super 404, message, details
108
122
  end
109
123
  end
110
124
 
111
125
  class InternalServerError < Exception
112
- def initialize(message, body = '')
113
- super 500, message, body
126
+ def initialize(message, details = {})
127
+ super 500, message, details
114
128
  end
115
129
  end
116
130
 
117
- class InvalidToken < Unauthorized; end
118
-
119
- class InvalidSession < InvalidToken; end
120
-
121
- class InvalidRequest < BadRequest; end
122
-
123
- class CreativeNotSaved < InternalServerError; end
124
-
125
- class QueryLockTimeout < InternalServerError; end
126
-
127
- class TargetingSpecNotSaved < InternalServerError; end
128
-
129
- class AdgroupFetchFailure < InternalServerError; end
130
-
131
- class OpenProcessFailure < InternalServerError; end
132
-
131
+ class InvalidToken < Unauthorized; end
132
+ class InvalidSession < InvalidToken; end
133
+ class InvalidRequest < BadRequest; end
134
+ class CreativeNotSaved < InternalServerError; end
135
+ class QueryLockTimeout < InternalServerError; end
136
+ class TargetingSpecNotSaved < InternalServerError; end
137
+ class AdgroupFetchFailure < InternalServerError; end
138
+ class OpenProcessFailure < InternalServerError; end
133
139
  class TransactionCommitFailure < InternalServerError; end
134
-
135
- class QueryError < InternalServerError; end
136
-
137
- class QueryConnection < InternalServerError; end
138
-
139
- class QueryDuplicateKey < InternalServerError; end
140
- end
140
+ class QueryError < InternalServerError; end
141
+ class QueryConnection < InternalServerError; end
142
+ class QueryDuplicateKey < InternalServerError; end
143
+ end
@@ -150,7 +150,7 @@ module FbGraph
150
150
  if (200...300).include?(response.status)
151
151
  _response_
152
152
  else
153
- Exception.handle_httpclient_error(_response_, response.headers)
153
+ Exception.handle_structured_response(response.status, _response_, response.headers)
154
154
  end
155
155
  end
156
156
  rescue MultiJson::DecodeError
@@ -7,16 +7,19 @@ describe FbGraph::Exception do
7
7
  end
8
8
 
9
9
  context 'when response body is given' do
10
- it 'should setup message and type from error' do
10
+ it 'should setup type, error code, and subcode from error' do
11
11
  err = FbGraph::Exception.new(400, 'This is the original message', {
12
12
  :error => {
13
13
  :type => 'SomeError',
14
- :message => 'This is the error message'
14
+ :message => 'This is the error message',
15
+ :code => 190,
16
+ :error_subcode => 460
15
17
  }
16
- }.to_json)
18
+ })
17
19
  err.code.should == 400
18
20
  err.type.should == 'SomeError'
19
- err.message.should == 'This is the error message'
21
+ err.error_code.should == 190
22
+ err.error_subcode.should == 460
20
23
  end
21
24
  end
22
25
 
@@ -28,157 +31,189 @@ describe FbGraph::Exception do
28
31
  err.message.should == 'This is the original message'
29
32
  end
30
33
  end
31
- end
32
-
33
- describe FbGraph::BadRequest do
34
- it 'should have 400 status code' do
35
- err = FbGraph::BadRequest.new 'Bad Request'
36
- err.code.should == 400
37
- end
38
- end
39
-
40
- describe FbGraph::Unauthorized do
41
- it 'should have 401 status code' do
42
- err = FbGraph::Unauthorized.new 'Unauthorized'
43
- err.code.should == 401
44
- end
45
- end
46
-
47
- describe FbGraph::NotFound do
48
- it 'should have 404 status code' do
49
- err = FbGraph::NotFound.new 'Not Found'
50
- err.code.should == 404
51
- end
52
- end
53
34
 
54
- describe FbGraph::Exception, ".handle_httpclient_error" do
55
- context "when WWW-Authenticate header is present" do
56
- context "with an expired access token" do
57
- let(:parsed_response) do
58
- {
59
- :error => {
60
- :message => "Error validating access token: The session has been invalidated because the user has changed the password.",
61
- :type => "OAuthException"
35
+ describe ".handle_structured_response" do
36
+ context "with WWW-Authenticate header" do
37
+ context "when token expired" do
38
+ let(:details) do
39
+ {
40
+ :error => {
41
+ :message => "Error validating access token: The session has been invalidated because the user has changed the password.",
42
+ :type => "OAuthException"
43
+ }
62
44
  }
63
- }
64
- end
65
- let(:headers) do
66
- {
67
- "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Error validating access token: The session has been invalidated because the user has changed the password.'
68
- }
69
- end
45
+ end
46
+ let(:headers) do
47
+ {
48
+ "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Error validating access token: The session has been invalidated because the user has changed the password.'
49
+ }
50
+ end
70
51
 
71
- it "should raise an InvalidSession exception" do
72
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidSession)
52
+ it do
53
+ expect do
54
+ FbGraph::Exception.handle_structured_response(999, details, headers)
55
+ end.to raise_exception FbGraph::InvalidSession
56
+ end
73
57
  end
74
- end
75
58
 
76
- context "with an invalid access token" do
77
- let(:parsed_response) do
78
- {
79
- :error => {
80
- :message => 'Invalid OAuth access token.',
81
- :type => "OAuthException"
59
+ context "when token invalid" do
60
+ let(:details) do
61
+ {
62
+ :error => {
63
+ :message => 'Invalid OAuth access token.',
64
+ :type => "OAuthException"
65
+ }
82
66
  }
83
- }
84
- end
85
- let(:headers) do
86
- {
87
- "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'
88
- }
89
- end
67
+ end
68
+ let(:headers) do
69
+ {
70
+ "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'
71
+ }
72
+ end
73
+
74
+ it do
75
+ expect do
76
+ FbGraph::Exception.handle_structured_response(999, details, headers)
77
+ end.to raise_exception FbGraph::InvalidToken
78
+ end
79
+
80
+ context "with a variant capitalization of the header" do
81
+ let(:headers) do
82
+ {
83
+ "Www-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'
84
+ }
85
+ end
90
86
 
91
- it "should raise an InvalidToken exception" do
92
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidToken)
87
+ it do
88
+ expect do
89
+ FbGraph::Exception.handle_structured_response(999, details, headers)
90
+ end.to raise_exception FbGraph::InvalidToken
91
+ end
92
+ end
93
93
  end
94
94
 
95
- context "with a variant capitalization of the header" do
95
+ context "with an invalid request" do
96
+ let(:details) do
97
+ {
98
+ :error => {
99
+ :message => '(#100) Must include the \"campaign_id\" index',
100
+ :type => "OAuthException"
101
+ }
102
+ }
103
+ end
96
104
  let(:headers) do
97
105
  {
98
- "Www-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'
106
+ "WWW-Authenticate" =>'OAuth "Facebook Platform" "invalid_request" "(#100) Must include the \"campaign_id\" index"'
99
107
  }
100
108
  end
101
109
 
102
- it "should raise an InvalidToken exception" do
103
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidToken)
110
+ it do
111
+ expect do
112
+ FbGraph::Exception.handle_structured_response(999, details, headers)
113
+ end.to raise_exception FbGraph::InvalidRequest
104
114
  end
105
115
  end
106
- end
107
116
 
108
- context "with an invalid request" do
109
- let(:parsed_response) do
110
- {
111
- :error => {
112
- :message => '(#100) Must include the \"campaign_id\" index',
113
- :type => "OAuthException"
117
+ context "to an an alias that does not exist" do
118
+ let(:details) do
119
+ {
120
+ :error => {
121
+ :message => '(#803) Some of the aliases you requested do not exist: test',
122
+ :type => "OAuthException"
123
+ }
114
124
  }
115
- }
116
- end
117
- let(:headers) do
118
- {
119
- "WWW-Authenticate" =>'OAuth "Facebook Platform" "invalid_request" "(#100) Must include the \"campaign_id\" index"'
120
- }
121
- end
125
+ end
126
+ let(:headers) do
127
+ {
128
+ "WWW-Authenticate" =>'OAuth "Facebook Platform" "not_found" "(#803) Some of the aliases you requested do not exist: test"'
129
+ }
130
+ end
122
131
 
123
- it "should raise an InvalidRequest exception" do
124
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidRequest)
132
+ it do
133
+ expect do
134
+ FbGraph::Exception.handle_structured_response(999, details, headers)
135
+ end.to raise_exception FbGraph::NotFound
136
+ end
125
137
  end
126
138
  end
127
139
 
128
- context "to an an alias that does not exist" do
129
- let(:parsed_response) do
130
- {
131
- :error => {
132
- :message => '(#803) Some of the aliases you requested do not exist: test',
133
- :type => "OAuthException"
134
- }
135
- }
136
- end
140
+ context "without the WWW-Authenticate header" do
137
141
  let(:headers) do
138
- {
139
- "WWW-Authenticate" =>'OAuth "Facebook Platform" "not_found" "(#803) Some of the aliases you requested do not exist: test"'
140
- }
142
+ {}
141
143
  end
142
144
 
143
- it "should raise a NotFound exception" do
144
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::NotFound)
145
+ context "with an OAuthException" do
146
+ let(:details) do
147
+ {
148
+ :error => {
149
+ :message => 'Some kind of OAuthException',
150
+ :type => "OAuthException"
151
+ }
152
+ }
153
+ end
154
+
155
+ it do
156
+ expect do
157
+ FbGraph::Exception.handle_structured_response(999, details, headers)
158
+ end.to raise_exception FbGraph::Unauthorized
159
+ end
145
160
  end
146
- end
147
- end
148
161
 
149
- context "without the WWW-Authenticate header" do
150
- context "with an OAuthException" do
151
- let(:parsed_response) do
152
- {
153
- :error => {
154
- :message => 'Some kind of OAuthException',
155
- :type => "OAuthException"
162
+ context "with a general exception" do
163
+ let(:details) do
164
+ {
165
+ :error => {
166
+ :message => 'Some kind of Exception',
167
+ :type => "Exception"
168
+ }
156
169
  }
157
- }
158
- end
170
+ end
159
171
 
160
- it "should raise an Unauthorized exception" do
161
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::Unauthorized)
172
+ {
173
+ 400 => FbGraph::BadRequest,
174
+ 401 => FbGraph::Unauthorized,
175
+ 404 => FbGraph::NotFound,
176
+ 500 => FbGraph::InternalServerError,
177
+ 999 => FbGraph::Exception
178
+ }.each do |status, exception_klass|
179
+ context "when status=#{status}" do
180
+ it do
181
+ expect do
182
+ FbGraph::Exception.handle_structured_response(status, details, headers)
183
+ end.to raise_exception exception_klass
184
+ end
185
+ end
186
+ end
162
187
  end
163
- end
164
188
 
165
- context "with an Exception" do
166
- let(:parsed_response) do
167
- {
168
- :error => {
169
- :message => 'Some kind of Exception',
170
- :type => "Exception"
189
+ context "with an unknown exception" do
190
+ let(:details) do
191
+ {
192
+ :error_code => 1,
193
+ :error_msg => "An unknown error occurred."
171
194
  }
172
- }
173
- end
195
+ end
174
196
 
175
- it "should raise a BadRequest exception" do
176
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::BadRequest)
197
+ {
198
+ 400 => FbGraph::BadRequest,
199
+ 401 => FbGraph::Unauthorized,
200
+ 404 => FbGraph::NotFound,
201
+ 500 => FbGraph::InternalServerError,
202
+ 999 => FbGraph::Exception
203
+ }.each do |status, exception_klass|
204
+ context "when status=#{status}" do
205
+ it do
206
+ expect do
207
+ FbGraph::Exception.handle_structured_response(status, details, headers)
208
+ end.to raise_exception exception_klass
209
+ end
210
+ end
211
+ end
177
212
  end
178
213
 
179
- context "with an Ads API Exception" do
214
+ describe "Ads API Exception" do
180
215
  context "of Could not save creative" do
181
- let(:parsed_response) do
216
+ let(:details) do
182
217
  {
183
218
  :error => {
184
219
  :message => 'Could not save creative',
@@ -187,88 +222,102 @@ describe FbGraph::Exception, ".handle_httpclient_error" do
187
222
  }
188
223
  end
189
224
 
190
- it "should raise a CreativeNotSaved exception" do
191
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::CreativeNotSaved)
225
+ it do
226
+ expect do
227
+ FbGraph::Exception.handle_structured_response(999, details, headers)
228
+ end.to raise_exception FbGraph::CreativeNotSaved
192
229
  end
193
230
  end
194
231
 
195
- context "of QueryLockTimeout" do
196
- let(:parsed_response) do
232
+ context "of Could not create targeting spec" do
233
+ let(:details) do
197
234
  {
198
235
  :error => {
199
- :message => 'QueryLockTimeoutException',
236
+ :message => 'Could not create targeting spec',
200
237
  :type => "Exception"
201
238
  }
202
239
  }
203
240
  end
204
241
 
205
- it "should raise a QueryLockTimeout exception" do
206
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::QueryLockTimeout)
242
+ it do
243
+ expect do
244
+ FbGraph::Exception.handle_structured_response(999, details, headers)
245
+ end.to raise_exception FbGraph::TargetingSpecNotSaved
207
246
  end
208
247
  end
209
248
 
210
- context "of Could not create targeting spec" do
211
- let(:parsed_response) do
249
+ context "of Could not fetch adgroups" do
250
+ let(:details) do
212
251
  {
213
252
  :error => {
214
- :message => 'Could not create targeting spec',
253
+ :message => 'Could not fetch adgroups',
215
254
  :type => "Exception"
216
255
  }
217
256
  }
218
257
  end
219
258
 
220
- it "should raise a TargetingSpecNotSaved exception" do
221
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::TargetingSpecNotSaved)
259
+ it do
260
+ expect do
261
+ FbGraph::Exception.handle_structured_response(999, details, headers)
262
+ end.to raise_exception FbGraph::AdgroupFetchFailure
222
263
  end
223
264
  end
224
265
 
225
- context "of Could not fetch adgroups" do
226
- let(:parsed_response) do
266
+ context "of Failed to open process" do
267
+ let(:details) do
227
268
  {
228
269
  :error => {
229
- :message => 'Could not fetch adgroups',
270
+ :message => 'Failed to open process',
230
271
  :type => "Exception"
231
272
  }
232
273
  }
233
274
  end
234
275
 
235
- it "should raise a AdgroupFetchFailure exception" do
236
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::AdgroupFetchFailure)
276
+ it do
277
+ expect do
278
+ FbGraph::Exception.handle_structured_response(999, details, headers)
279
+ end.to raise_exception FbGraph::OpenProcessFailure
237
280
  end
238
281
  end
239
282
 
240
- context "of Failed to open process" do
241
- let(:parsed_response) do
283
+ context "of Could not commit transaction" do
284
+ let(:details) do
242
285
  {
243
286
  :error => {
244
- :message => 'Failed to open process',
287
+ :message => 'Could not commit transaction',
245
288
  :type => "Exception"
246
289
  }
247
290
  }
248
291
  end
249
292
 
250
- it "should raise a OpenProcessFailure exception" do
251
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::OpenProcessFailure)
293
+ it do
294
+ expect do
295
+ FbGraph::Exception.handle_structured_response(999, details, headers)
296
+ end.to raise_exception FbGraph::TransactionCommitFailure
252
297
  end
253
298
  end
299
+ end
254
300
 
255
- context "of Could not commit transaction" do
256
- let(:parsed_response) do
301
+ describe 'FQL Exceptions' do
302
+ context "of QueryLockTimeout" do
303
+ let(:details) do
257
304
  {
258
305
  :error => {
259
- :message => 'Could not commit transaction',
306
+ :message => 'QueryLockTimeoutException',
260
307
  :type => "Exception"
261
308
  }
262
309
  }
263
310
  end
264
311
 
265
- it "should raise a TransactionCommitFailure exception" do
266
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::TransactionCommitFailure)
312
+ it do
313
+ expect do
314
+ FbGraph::Exception.handle_structured_response(999, details, headers)
315
+ end.to raise_exception FbGraph::QueryLockTimeout
267
316
  end
268
317
  end
269
318
 
270
319
  context "of QueryErrorException" do
271
- let(:parsed_response) do
320
+ let(:details) do
272
321
  {
273
322
  :error => {
274
323
  :message => 'DB Error: QueryErrorException',
@@ -277,13 +326,15 @@ describe FbGraph::Exception, ".handle_httpclient_error" do
277
326
  }
278
327
  end
279
328
 
280
- it "should raise a QueryError exception" do
281
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::QueryError)
329
+ it do
330
+ expect do
331
+ FbGraph::Exception.handle_structured_response(999, details, headers)
332
+ end.to raise_exception FbGraph::QueryError
282
333
  end
283
334
  end
284
335
 
285
336
  context "of QueryConnectionException" do
286
- let(:parsed_response) do
337
+ let(:details) do
287
338
  {
288
339
  :error => {
289
340
  :message => 'QueryConnectionException',
@@ -292,13 +343,15 @@ describe FbGraph::Exception, ".handle_httpclient_error" do
292
343
  }
293
344
  end
294
345
 
295
- it "should raise a QueryConnection exception" do
296
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::QueryConnection)
346
+ it do
347
+ expect do
348
+ FbGraph::Exception.handle_structured_response(999, details, headers)
349
+ end.to raise_exception FbGraph::QueryConnection
297
350
  end
298
351
  end
299
352
 
300
353
  context "of QueryDuplicateKeyException" do
301
- let(:parsed_response) do
354
+ let(:details) do
302
355
  {
303
356
  :error => {
304
357
  :message => 'QueryDuplicateKeyException',
@@ -307,24 +360,34 @@ describe FbGraph::Exception, ".handle_httpclient_error" do
307
360
  }
308
361
  end
309
362
 
310
- it "should raise a QueryDuplicateKey exception" do
311
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::QueryDuplicateKey)
363
+ it do
364
+ expect do
365
+ FbGraph::Exception.handle_structured_response(999, details, headers)
366
+ end.to raise_exception FbGraph::QueryDuplicateKey
312
367
  end
313
368
  end
314
369
  end
315
370
  end
316
- context "with an Exception not containing the error field" do
317
- let(:parsed_response) do
318
- {
319
- :error_code => 1,
320
- :error_msg => "An unknown error occurred."
321
- }
322
- end
371
+ end
372
+ end
323
373
 
324
- it "should raise an InternalServerError exception" do
325
- lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::InternalServerError)
326
- end
374
+ describe FbGraph::BadRequest do
375
+ it 'should have 400 status code' do
376
+ err = FbGraph::BadRequest.new 'Bad Request'
377
+ err.code.should == 400
378
+ end
379
+ end
327
380
 
328
- end
381
+ describe FbGraph::Unauthorized do
382
+ it 'should have 401 status code' do
383
+ err = FbGraph::Unauthorized.new 'Unauthorized'
384
+ err.code.should == 401
385
+ end
386
+ end
387
+
388
+ describe FbGraph::NotFound do
389
+ it 'should have 404 status code' do
390
+ err = FbGraph::NotFound.new 'Not Found'
391
+ err.code.should == 404
329
392
  end
330
393
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fb_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.3
4
+ version: 2.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov matake