handle 401's

This commit is contained in:
stve 2016-01-14 22:42:34 -05:00
parent da254e4aab
commit cb1d2348a6
3 changed files with 19 additions and 2 deletions

View file

@ -11,6 +11,7 @@ module Instapaper
OAuthError = Class.new(self)
ERRORS = {
401 => 'Unauthorized',
1040 => 'Rate-limit exceeded',
1041 => 'Premium account required',
1042 => 'Application is suspended',

View file

@ -49,13 +49,19 @@ module Instapaper
end
def fail_if_error_response_code(response)
fail Instapaper::Error::ServiceUnavailableError if response.status != 200
return if response.status == 200
if Instapaper::Error::CODES.include?(response.status.code)
fail Instapaper::Error.from_response(response.status.code, @path)
else
fail Instapaper::Error::ServiceUnavailableError
end
end
def fail_if_error_unparseable_response(response)
response.parse(:json)
rescue JSON::ParserError
raise Instapaper::Error::ServiceUnavailableError
fail Instapaper::Error::ServiceUnavailableError
end
def fail_if_error_in_body(response)

View file

@ -70,4 +70,14 @@ describe Instapaper::Error do
end
end
end
context 'HTTP errors' do
before do
stub_post('/api/1.1/oauth/access_token')
.to_return(status: 401, body: 'Unauthorized', headers: {})
end
it 'raises an Instapaper::Error' do
expect { @client.access_token('foo', 'bar') }.to raise_error(Instapaper::Error)
end
end
end