From 6faa71a965c00ee2f78d8fd8183d34197caa7ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Hornburg?= Date: Fri, 16 Aug 2019 17:38:22 -0300 Subject: [PATCH 1/4] handle errors correctly when the server returns a html response --- lib/rdstation/api_response.rb | 3 +- lib/rdstation/error_handler.rb | 2 ++ spec/lib/rdstation/api_response_spec.rb | 40 ++++++++++++++++++++++++ spec/lib/rdstation/error_handler_spec.rb | 14 +++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 spec/lib/rdstation/api_response_spec.rb diff --git a/lib/rdstation/api_response.rb b/lib/rdstation/api_response.rb index ef4e158..8da4294 100644 --- a/lib/rdstation/api_response.rb +++ b/lib/rdstation/api_response.rb @@ -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 diff --git a/lib/rdstation/error_handler.rb b/lib/rdstation/error_handler.rb index a5c2690..5a435e1 100644 --- a/lib/rdstation/error_handler.rb +++ b/lib/rdstation/error_handler.rb @@ -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 diff --git a/spec/lib/rdstation/api_response_spec.rb b/spec/lib/rdstation/api_response_spec.rb new file mode 100644 index 0000000..c3c9267 --- /dev/null +++ b/spec/lib/rdstation/api_response_spec.rb @@ -0,0 +1,40 @@ +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: '') } + + it "raises no error" do + expect do + RDStation::ApiResponse.build(response) + end.not_to raise_error + end + + it_behaves_like 'call_error_handler' + end + end +end diff --git a/spec/lib/rdstation/error_handler_spec.rb b/spec/lib/rdstation/error_handler_spec.rb index aefbb92..d08b0ee 100644 --- a/spec/lib/rdstation/error_handler_spec.rb +++ b/spec/lib/rdstation/error_handler_spec.rb @@ -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 error response' + ) + end + + it 'raises the correct error' do + expect { error_handler.raise_error }.to raise_error(RDStation::Error::BadGateway, 'HTML error response') + end + end end end From d70ba1a2ad959ae0dda7722608f9a455d191e2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Hornburg?= Date: Fri, 16 Aug 2019 17:45:35 -0300 Subject: [PATCH 2/4] bump version and update chagelog --- CHANGELOG.md | 4 ++++ lib/rdstation/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46d0dbe..0558cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rdstation/version.rb b/lib/rdstation/version.rb index d11d34b..ac8b43f 100644 --- a/lib/rdstation/version.rb +++ b/lib/rdstation/version.rb @@ -1,3 +1,3 @@ module RDStation - VERSION = '2.1.0'.freeze + VERSION = '2.1.1'.freeze end From 21bd338aec818e61c4df9f864130ed769262a643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Hornburg?= Date: Fri, 16 Aug 2019 18:19:04 -0300 Subject: [PATCH 3/4] fix spec --- spec/lib/rdstation/api_response_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spec/lib/rdstation/api_response_spec.rb b/spec/lib/rdstation/api_response_spec.rb index c3c9267..6ab38ab 100644 --- a/spec/lib/rdstation/api_response_spec.rb +++ b/spec/lib/rdstation/api_response_spec.rb @@ -28,12 +28,6 @@ context "when the response body is not JSON-parseable" do let(:response) { OpenStruct.new(code: 504, body: '') } - it "raises no error" do - expect do - RDStation::ApiResponse.build(response) - end.not_to raise_error - end - it_behaves_like 'call_error_handler' end end From 8a8e036e73be8be5160c7a5f0f7e1c0c1d3f142b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Hornburg?= Date: Tue, 3 Sep 2019 15:00:08 -0300 Subject: [PATCH 4/4] empty spaces --- lib/rdstation/error_handler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rdstation/error_handler.rb b/lib/rdstation/error_handler.rb index 5a435e1..209bea8 100644 --- a/lib/rdstation/error_handler.rb +++ b/lib/rdstation/error_handler.rb @@ -14,7 +14,7 @@ def raise_error error_class.new(array_of_errors).raise_error rescue JSON::ParserError => error - raise error_class, {'error_message' => response.body} + raise error_class, { 'error_message' => response.body } end private