Catch json parsing error as PrometheusError

This commit is contained in:
Pawel Chojnacki 2018-02-07 02:25:54 +01:00
parent 5209689d95
commit 0e90284c11
2 changed files with 12 additions and 0 deletions

View File

@ -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

View File

@ -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