mirror of
https://github.com/samsonjs/instapaper.git
synced 2026-03-25 08:55:49 +00:00
fix error handling
errors are passed in the response body, not by response code
This commit is contained in:
parent
f745e5bc73
commit
9c2babc55f
2 changed files with 24 additions and 10 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue