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/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..209bea8 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/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 diff --git a/spec/lib/rdstation/api_response_spec.rb b/spec/lib/rdstation/api_response_spec.rb new file mode 100644 index 0000000..6ab38ab --- /dev/null +++ b/spec/lib/rdstation/api_response_spec.rb @@ -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: '') } + + 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