separate out error handling methods

This commit is contained in:
stve 2015-03-01 22:59:00 -05:00
parent 6509837fc1
commit f6fca37fd9
2 changed files with 14 additions and 14 deletions

View file

@ -42,19 +42,23 @@ module Instapaper
options_key = @request_method == :get ? :params : :form
response = ::HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
fail_if_error(response, raw)
fail_if_error_in_body(parsed_response(response))
raw ? response.to_s : parsed_response(response)
end
def fail_if_error(response, raw)
fail Instapaper::Error::ServiceUnavailableError if response.status != 200
return if raw
fail_if_error_response_code(response)
fail_if_error_unparseable_response(response) unless raw
fail_if_error_in_body(parsed_response(response))
end
begin
def fail_if_error_response_code(response)
fail Instapaper::Error::ServiceUnavailableError if response.status != 200
end
def fail_if_error_unparseable_response(response)
response.parse
rescue JSON::ParserError
fail Instapaper::Error::ServiceUnavailableError
end
raise Instapaper::Error::ServiceUnavailableError
end
def fail_if_error_in_body(response)

View file

@ -10,9 +10,7 @@ describe Instapaper::HTTP::Request do
.to_return(status: 503, body: '', headers: {content_type: 'application/json; charset=utf-8'})
end
it 'raises a ServiceUnavailableError' do
expect {
client.folders
}.to raise_error(Instapaper::Error::ServiceUnavailableError)
expect { client.folders }.to raise_error(Instapaper::Error::ServiceUnavailableError)
end
end
@ -22,9 +20,7 @@ describe Instapaper::HTTP::Request do
.to_return(status: 200, body: '{"key":"value}', headers: {content_type: 'application/json; charset=utf-8'})
end
it 'raises a ServiceUnavailableError' do
expect {
client.folders
}.to raise_error(Instapaper::Error::ServiceUnavailableError)
expect { client.folders }.to raise_error(Instapaper::Error::ServiceUnavailableError)
end
end
end