summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/gitlab/prometheus_client.rb2
-rw-r--r--spec/lib/gitlab/prometheus_client_spec.rb10
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/gitlab/prometheus_client.rb b/lib/gitlab/prometheus_client.rb
index 8264501b1ae..10527972663 100644
--- a/lib/gitlab/prometheus_client.rb
+++ b/lib/gitlab/prometheus_client.rb
@@ -42,6 +42,8 @@ module Gitlab
def json_api_get(type, args = {})
path = ['api', 'v1', type].join('/')
get(path, args)
+ rescue JSON::ParserError
+ raise PrometheusError, 'Parsing response failed'
rescue Errno::ECONNREFUSED
raise PrometheusError, 'Connection refused'
end
diff --git a/spec/lib/gitlab/prometheus_client_spec.rb b/spec/lib/gitlab/prometheus_client_spec.rb
index 575b7805e33..5d86007f71f 100644
--- a/spec/lib/gitlab/prometheus_client_spec.rb
+++ b/spec/lib/gitlab/prometheus_client_spec.rb
@@ -47,6 +47,16 @@ describe Gitlab::PrometheusClient do
expect(req_stub).to have_been_requested
end
end
+
+ context 'when request returns non json data' do
+ it 'raises a Gitlab::PrometheusError error' do
+ req_stub = stub_prometheus_request(query_url, status: 200, body: 'not json')
+
+ expect { execute_query }
+ .to raise_error(Gitlab::PrometheusError, 'Parsing response failed')
+ expect(req_stub).to have_been_requested
+ end
+ end
end
describe 'failure to reach a provided prometheus url' do