Handle invalid JSON from server

This commit is contained in:
Stan Hu 2018-07-27 14:29:05 -07:00
parent 450030e902
commit 57d1b60f61
2 changed files with 13 additions and 6 deletions

View File

@ -59,16 +59,17 @@ module BitbucketServer
private
def check_errors!(response)
raise ConnectionError, "Response is not valid JSON" unless response.parsed_response.is_a?(Hash)
return if response.code >= 200 && response.code < 300
details =
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 += ": #{details}" if details
message += ": #{details}"
raise ConnectionError, message
rescue JSON::ParserError
raise ConnectionError, "Unable to parse the server response as JSON"
end
def auth

View File

@ -20,6 +20,12 @@ describe BitbucketServer::Connection do
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
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
describe '#post' do