summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-07-27 14:29:05 -0700
committerStan Hu <stanhu@gmail.com>2018-07-27 14:29:05 -0700
commit57d1b60f61a790c034b8d43ce2358371464d2d67 (patch)
treec1e625ab9ec32a89f51ca966c1b3245d3db1a1b6
parent450030e9029f5de6fcfb850e5940838c2a76c81e (diff)
downloadgitlab-ce-57d1b60f61a790c034b8d43ce2358371464d2d67.tar.gz
Handle invalid JSON from server
-rw-r--r--lib/bitbucket_server/connection.rb13
-rw-r--r--spec/lib/bitbucket_server/connection_spec.rb6
2 files changed, 13 insertions, 6 deletions
diff --git a/lib/bitbucket_server/connection.rb b/lib/bitbucket_server/connection.rb
index 2d438ae8ca1..ee0888eeecf 100644
--- a/lib/bitbucket_server/connection.rb
+++ b/lib/bitbucket_server/connection.rb
@@ -59,16 +59,17 @@ module BitbucketServer
private
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 =
- if response.parsed_response && response.parsed_response.is_a?(Hash)
- sanitize(response.parsed_response.dig('errors', 0, 'message'))
- end
+ return if response.code >= 200 && response.code < 300
+ 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
diff --git a/spec/lib/bitbucket_server/connection_spec.rb b/spec/lib/bitbucket_server/connection_spec.rb
index 33a037218a0..b5da4cb1a49 100644
--- a/spec/lib/bitbucket_server/connection_spec.rb
+++ b/spec/lib/bitbucket_server/connection_spec.rb
@@ -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