Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug 47 handle html responses #48

Merged
merged 4 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are handling this error directly in the API response, in which cases that exception would be raised?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be raised when the body ain't a valid JSON. The most comon scenario for this to happen is when we have a downtime on the API.

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