mirror of
https://github.com/nov/fb_graph2
synced 2023-03-27 23:22:15 -04:00
added error_code alias for code and error_subcode to FbGraph2::Exception
This commit is contained in:
parent
04f287c01b
commit
181e023bc0
2 changed files with 47 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
module FbGraph2
|
||||
class Exception < StandardError
|
||||
attr_accessor :status, :type, :code
|
||||
attr_accessor :status, :type, :code, :error_subcode
|
||||
alias_method :error_code, :code
|
||||
|
||||
class << self
|
||||
def detect(status, body = {}, headers = {})
|
||||
|
@ -43,6 +44,7 @@ module FbGraph2
|
|||
self.status = status
|
||||
self.type = error[:type]
|
||||
self.code = error[:code]
|
||||
self.error_subcode = error[:error_subcode]
|
||||
end
|
||||
|
||||
class BadRequest < Exception
|
||||
|
@ -78,4 +80,4 @@ module FbGraph2
|
|||
/invalid_request/ => InvalidRequest
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
43
spec/fb_graph2/exception_spec.rb
Normal file
43
spec/fb_graph2/exception_spec.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe FbGraph2::Exception do
|
||||
it 'should properly set its message for inspect' do
|
||||
err = FbGraph2::Exception.new(400, 'This is the error message')
|
||||
err.inspect.should == '#<FbGraph2::Exception: This is the error message>'
|
||||
end
|
||||
|
||||
context 'when response body is given' do
|
||||
it 'should setup type, error code, and subcode from error' do
|
||||
err = FbGraph2::Exception.new(400, 'This is the original message',
|
||||
{
|
||||
:type => 'SomeError',
|
||||
:message => 'This is the error message',
|
||||
:code => 190,
|
||||
:error_subcode => 460
|
||||
}
|
||||
)
|
||||
err.status.should == 400
|
||||
err.type.should == 'SomeError'
|
||||
err.error_code.should == 190
|
||||
err.error_subcode.should == 460
|
||||
end
|
||||
end
|
||||
|
||||
describe ".detect" do
|
||||
it 'should detect the appropriate class from the error status' do
|
||||
[400,401,404,500].each do |error_code|
|
||||
err = FbGraph2::Exception.detect(error_code, error: { message: "Error #{error_code}"})
|
||||
err.class.should == FbGraph2::Exception.detect_from_status(error_code)
|
||||
end
|
||||
end
|
||||
|
||||
it 'should detect the appropriate class from headers' do
|
||||
%w{not_found invalid_token, invalid_request}.each do |error|
|
||||
header = {"WWW-Authenticate" => "OAuth 'Facebook Platform' '#{error}' Error!"}
|
||||
err = FbGraph2::Exception.detect('a code', {error: {message: "an error occurred"}}, header)
|
||||
err.class.should == FbGraph2::Exception.detect_from_header(header, nil)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue