Skip to content

Commit

Permalink
fix: JSON parser error on timeout response (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maychell authored Apr 26, 2021
1 parent 6b7c1d8 commit 3ee9e8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/twilio-ruby/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def _request(request)
@last_response = nil

response = send(request)
if response.body && !response.body.empty?
if response.status == 504
object = { message: 'Request timeout', code: 504 }.to_json
elsif response.body && !response.body.empty?
object = response.body
elsif response.status == 400
object = { message: 'Bad request', code: 400 }.to_json
Expand Down
16 changes: 16 additions & 0 deletions spec/http/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,20 @@
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Twilio::REST::TwilioError)
expect(@client.last_response).to be_nil
end

context 'when the response returns a timeout' do
let(:twilio_response) { @client.request('host', 'port', 'GET', 'url') }

before do
faraday_connection = Faraday::Connection.new

allow(Faraday).to receive(:new).and_return(faraday_connection)
allow(faraday_connection).to receive(:send).and_return(double('response', status: 504, body: { message: 'any message' }, headers: {}))
end

it 'adds a Request timeout message to the response and avoids non-JSON response to raise Json::ParserError' do
expect(twilio_response.body).to eq({ 'message' => 'Request timeout', 'code' => 504 })
expect(twilio_response.status_code).to eq 504
end
end
end

0 comments on commit 3ee9e8f

Please sign in to comment.