mirror of
https://github.com/samsonjs/instapaper.git
synced 2026-04-27 14:57:44 +00:00
separate out error handling methods
This commit is contained in:
parent
6509837fc1
commit
f6fca37fd9
2 changed files with 14 additions and 14 deletions
|
|
@ -42,19 +42,23 @@ module Instapaper
|
||||||
options_key = @request_method == :get ? :params : :form
|
options_key = @request_method == :get ? :params : :form
|
||||||
response = ::HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
|
response = ::HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
|
||||||
fail_if_error(response, raw)
|
fail_if_error(response, raw)
|
||||||
fail_if_error_in_body(parsed_response(response))
|
|
||||||
raw ? response.to_s : parsed_response(response)
|
raw ? response.to_s : parsed_response(response)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fail_if_error(response, raw)
|
def fail_if_error(response, raw)
|
||||||
fail Instapaper::Error::ServiceUnavailableError if response.status != 200
|
fail_if_error_response_code(response)
|
||||||
return if raw
|
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)
|
||||||
response.parse
|
fail Instapaper::Error::ServiceUnavailableError if response.status != 200
|
||||||
rescue JSON::ParserError
|
end
|
||||||
fail Instapaper::Error::ServiceUnavailableError
|
|
||||||
end
|
def fail_if_error_unparseable_response(response)
|
||||||
|
response.parse
|
||||||
|
rescue JSON::ParserError
|
||||||
|
raise Instapaper::Error::ServiceUnavailableError
|
||||||
end
|
end
|
||||||
|
|
||||||
def fail_if_error_in_body(response)
|
def fail_if_error_in_body(response)
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,7 @@ describe Instapaper::HTTP::Request do
|
||||||
.to_return(status: 503, body: '', headers: {content_type: 'application/json; charset=utf-8'})
|
.to_return(status: 503, body: '', headers: {content_type: 'application/json; charset=utf-8'})
|
||||||
end
|
end
|
||||||
it 'raises a ServiceUnavailableError' do
|
it 'raises a ServiceUnavailableError' do
|
||||||
expect {
|
expect { client.folders }.to raise_error(Instapaper::Error::ServiceUnavailableError)
|
||||||
client.folders
|
|
||||||
}.to raise_error(Instapaper::Error::ServiceUnavailableError)
|
|
||||||
end
|
end
|
||||||
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'})
|
.to_return(status: 200, body: '{"key":"value}', headers: {content_type: 'application/json; charset=utf-8'})
|
||||||
end
|
end
|
||||||
it 'raises a ServiceUnavailableError' do
|
it 'raises a ServiceUnavailableError' do
|
||||||
expect {
|
expect { client.folders }.to raise_error(Instapaper::Error::ServiceUnavailableError)
|
||||||
client.folders
|
|
||||||
}.to raise_error(Instapaper::Error::ServiceUnavailableError)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue