summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPawel Chojnacki <pawel@chojnacki.ws>2018-02-07 02:25:54 +0100
committerPawel Chojnacki <pawel@chojnacki.ws>2018-02-07 02:25:54 +0100
commit0e90284c11815f84c804e7e922e709b31ca6d029 (patch)
tree73c04b5accf3f317b36a9c1ca2f00b5454547d5a
parent5209689d956c727f784481c86fe61355385371f5 (diff)
downloadgitlab-ce-pawel/connect_to_prometheus_through_proxy-30480.tar.gz
Catch json parsing error as PrometheusErrorpawel/connect_to_prometheus_through_proxy-30480
-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