Skip to content

Commit

Permalink
Merge pull request #48 from ResultadosDigitais/fix-bug-47-handle-html…
Browse files Browse the repository at this point in the history
…-responses

Fix bug 47 handle html responses
  • Loading branch information
joaohornburg authored Sep 17, 2019
2 parents b719623 + 8a8e036 commit e56a75d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.1

- Fixed a bug in error handling (issue [#47](https://github.com/ResultadosDigitais/rdstation-ruby-client/issues/47))

## 2.1.0

### Additions
Expand Down
3 changes: 1 addition & 2 deletions lib/rdstation/api_response.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module RDStation
module ApiResponse
def self.build(response)
response_body = JSON.parse(response.body)
return response_body if response.code.between?(200, 299)
return JSON.parse(response.body) if response.code.between?(200, 299)

RDStation::ErrorHandler.new(response).raise_error
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rdstation/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def raise_error
raise error_class, array_of_errors.first if error_class < RDStation::Error

error_class.new(array_of_errors).raise_error
rescue JSON::ParserError => error
raise error_class, { 'error_message' => response.body }
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/rdstation/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RDStation
VERSION = '2.1.0'.freeze
VERSION = '2.1.1'.freeze
end
34 changes: 34 additions & 0 deletions spec/lib/rdstation/api_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

RSpec.describe RDStation::ApiResponse do
describe ".build" do
context "when the response HTTP status is 2xx" do
let(:response) { OpenStruct.new(code: 200, body: '{}') }

it "returns the response body" do
expect(RDStation::ApiResponse.build(response)).to eq({})
end
end

shared_examples_for 'call_error_handler' do
it "calls error handler" do
error_handler = instance_double(RDStation::ErrorHandler)
allow(error_handler).to receive(:raise_error)
expect(RDStation::ErrorHandler).to receive(:new).with(response).and_return(error_handler)
RDStation::ApiResponse.build(response)
end
end

context "when the response is not in the 2xx range" do
let(:response) { OpenStruct.new(code: 404, body: '{}') }

it_behaves_like 'call_error_handler'
end

context "when the response body is not JSON-parseable" do
let(:response) { OpenStruct.new(code: 504, body: '<html><head></head><body></body></html>') }

it_behaves_like 'call_error_handler'
end
end
end
14 changes: 14 additions & 0 deletions spec/lib/rdstation/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,19 @@
expect { error_handler.raise_error }.to raise_error(RDStation::Error::ServerError, 'Error Message')
end
end

context "when response body is not JSON-parseable" do
let(:error_response) do
OpenStruct.new(
code: 502,
headers: { 'error' => 'header' },
body: '<html><body>HTML error response</body></html>'
)
end

it 'raises the correct error' do
expect { error_handler.raise_error }.to raise_error(RDStation::Error::BadGateway, '<html><body>HTML error response</body></html>')
end
end
end
end

0 comments on commit e56a75d

Please sign in to comment.