mirror of
https://github.com/samsonjs/instapaper.git
synced 2026-03-25 08:55:49 +00:00
handle 401's
This commit is contained in:
parent
da254e4aab
commit
cb1d2348a6
3 changed files with 19 additions and 2 deletions
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue