Commit 57d1b60f authored by Stan Hu's avatar Stan Hu

Handle invalid JSON from server

parent 450030e9
...@@ -59,16 +59,17 @@ module BitbucketServer ...@@ -59,16 +59,17 @@ module BitbucketServer
private private
def check_errors!(response) def check_errors!(response)
return if response.code >= 200 && response.code < 300 raise ConnectionError, "Response is not valid JSON" unless response.parsed_response.is_a?(Hash)
details = return if response.code >= 200 && response.code < 300
if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message'))
end
details = sanitize(response.parsed_response.dig('errors', 0, 'message'))
message = "Error #{response.code}" message = "Error #{response.code}"
message += ": #{details}" if details message += ": #{details}"
raise ConnectionError, message raise ConnectionError, message
rescue JSON::ParserError
raise ConnectionError, "Unable to parse the server response as JSON"
end end
def auth def auth
......
...@@ -20,6 +20,12 @@ describe BitbucketServer::Connection do ...@@ -20,6 +20,12 @@ describe BitbucketServer::Connection do
expect { subject.get(url) }.to raise_error(described_class::ConnectionError) expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
end end
it 'throws an exception if the response is not JSON' do
WebMock.stub_request(:get, url).with(headers: { 'Accept' => 'application/json' }).to_return(body: 'bad data', status: 200, headers: headers)
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
end
end end
describe '#post' do describe '#post' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment