Handle invalid JSON from server
This commit is contained in:
parent
450030e902
commit
57d1b60f61
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue