From f6fca37fd9cb66aad2aa0c5c7bb75035fd889589 Mon Sep 17 00:00:00 2001 From: stve Date: Sun, 1 Mar 2015 22:59:00 -0500 Subject: [PATCH] separate out error handling methods --- lib/instapaper/http/request.rb | 20 ++++++++++++-------- spec/instapaper/http/request_spec.rb | 8 ++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/instapaper/http/request.rb b/lib/instapaper/http/request.rb index 5666fd6..49cd4bd 100644 --- a/lib/instapaper/http/request.rb +++ b/lib/instapaper/http/request.rb @@ -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 - response.parse - rescue JSON::ParserError - fail Instapaper::Error::ServiceUnavailableError - end + 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 + raise Instapaper::Error::ServiceUnavailableError end def fail_if_error_in_body(response) diff --git a/spec/instapaper/http/request_spec.rb b/spec/instapaper/http/request_spec.rb index 8dd3f29..cf54938 100644 --- a/spec/instapaper/http/request_spec.rb +++ b/spec/instapaper/http/request_spec.rb @@ -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