From 9c2babc55faccda97e265accfb72f981a4e9f58c Mon Sep 17 00:00:00 2001 From: stve Date: Wed, 25 Feb 2015 22:22:13 -0500 Subject: [PATCH] fix error handling errors are passed in the response body, not by response code --- lib/instapaper/http/request.rb | 22 ++++++++++++++++------ spec/instapaper/error_spec.rb | 12 ++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/instapaper/http/request.rb b/lib/instapaper/http/request.rb index 1e7f3b6..437b19b 100644 --- a/lib/instapaper/http/request.rb +++ b/lib/instapaper/http/request.rb @@ -39,19 +39,29 @@ module Instapaper @headers = Instapaper::HTTP::Headers.new(@client, @request_method, @uri, @options).request_headers 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 ? response.to_s : symbolize_keys!(response.parse) + fail_if_error(parsed_response(response)) + raw ? response.to_s : parsed_response(response) end def fail_if_error(response) - error = error(response.code) + error = error(response) fail(error) if error end - def error(code) - return unless Instapaper::Error::CODES.index(code.to_i) + def error(response) + return unless response.is_a?(Array) + return unless response.size > 0 + return unless response.first[:type] == 'error' - Instapaper::Error.from_response(code, @path) + Instapaper::Error.from_response(response.first[:error_code], @path) + end + + def parsed_response(response) + @parsed_response ||= begin + symbolize_keys!(response.parse) + rescue + response.to_s + end end def symbolize_keys!(object) diff --git a/spec/instapaper/error_spec.rb b/spec/instapaper/error_spec.rb index d47c27a..ba9e862 100644 --- a/spec/instapaper/error_spec.rb +++ b/spec/instapaper/error_spec.rb @@ -21,9 +21,10 @@ describe Instapaper::Error do Instapaper::Error::ERRORS.each do |status, exception| context "when HTTP status is #{status}" do + let(:response_body) { %{[{"type":"error", "error_code":#{status}, "message":"Error Message URL"}]} } before do stub_post('/api/1/oauth/access_token') - .to_return(status: status, body: '', headers: {content_type: 'application/json; charset=utf-8'}) + .to_return(status: 200, body: response_body, headers: {content_type: 'application/json; charset=utf-8'}) end it "raises #{exception}" do expect { @client.access_token('foo', 'bar') }.to raise_error(Instapaper::Error) @@ -33,9 +34,10 @@ describe Instapaper::Error do Instapaper::Error::BOOKMARK_ERRORS.each do |status, exception| context "when HTTP status is #{status}" do + let(:response_body) { %{[{"type":"error", "error_code":#{status}, "message":"Error Message URL"}]} } before do stub_post('/api/1/bookmarks/list') - .to_return(status: status, body: '', headers: {content_type: 'application/json; charset=utf-8'}) + .to_return(status: 200, body: response_body, headers: {content_type: 'application/json; charset=utf-8'}) end it "raises #{exception}" do expect { @client.bookmarks }.to raise_error(Instapaper::Error::BookmarkError) @@ -45,9 +47,10 @@ describe Instapaper::Error do Instapaper::Error::FOLDER_ERRORS.each do |status, exception| context "when HTTP status is #{status}" do + let(:response_body) { %{[{"type":"error", "error_code":#{status}, "message":"Error Message URL"}]} } before do stub_post('/api/1/folders/list') - .to_return(status: status, body: '', headers: {content_type: 'application/json; charset=utf-8'}) + .to_return(status: 200, body: response_body, headers: {content_type: 'application/json; charset=utf-8'}) end it "raises #{exception}" do expect { @client.folders }.to raise_error(Instapaper::Error::FolderError) @@ -57,9 +60,10 @@ describe Instapaper::Error do Instapaper::Error::HIGHLIGHT_ERRORS.each do |status, exception| context "when HTTP status is #{status}" do + let(:response_body) { %{[{"type":"error", "error_code":#{status}, "message":"Error Message URL"}]} } before do stub_post('/api/1.1/bookmarks/123/highlights') - .to_return(status: status, body: fixture('highlights_list.json'), headers: {content_type: 'application/json; charset=utf-8'}) + .to_return(status: 200, body: response_body, headers: {content_type: 'application/json; charset=utf-8'}) end it "raises #{exception}" do expect { @client.highlights('123') }.to raise_error(Instapaper::Error::HighlightError)