Skip to content

Commit a0bee50

Browse files
authored
Merge pull request #25 from tgwizard/handle-http-api-error-responses
Handle HTTP API error responses
2 parents dcbf2ba + 0dafc9a commit a0bee50

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

lib/prometheus/api_client/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ def get(command, options)
104104
def run_command(command, options)
105105
response = get(command, options)
106106

107-
JSON.parse(response.body)['data']
107+
body = JSON.parse(response.body)
108+
raise RequestError, body['error'] if body['status'] != 'success'
109+
110+
body['data']
108111
rescue StandardError => err
109112
raise RequestError, err.message
110113
end

spec/prometheus/api_client/client_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,35 @@
4444
end
4545
end
4646
end
47+
48+
describe '.run_command' do
49+
it 'returns a hash' do
50+
VCR.use_cassette('prometheus/api_client/client') do # , record: :new_episodes) do
51+
prometheus = Prometheus::ApiClient::Client.new(
52+
url: url,
53+
credentials: { token: token },
54+
options: { verify_ssl: OpenSSL::SSL::VERIFY_NONE },
55+
)
56+
57+
response = prometheus.run_command(
58+
"query_range",
59+
{ query: "up", start: "2015-07-01T20:10:30.781Z", end: "2015-07-01T20:11:00.781Z", step: "15s"}
60+
)
61+
62+
expect(response).to eq({ "resultType"=>"matrix", "result"=>[], "explanation" => nil })
63+
end
64+
end
65+
66+
it 'raises on error' do
67+
VCR.use_cassette('prometheus/api_client/client') do # , record: :new_episodes) do
68+
prometheus = Prometheus::ApiClient::Client.new(
69+
url: url,
70+
credentials: { token: token },
71+
options: { verify_ssl: OpenSSL::SSL::VERIFY_NONE },
72+
)
73+
74+
expect { prometheus.run_command("query_ranage", { query: "(not a valid command"}) } .to raise_error(Prometheus::ApiClient::Client::RequestError)
75+
end
76+
end
77+
end
4778
end

spec/vcr_cassettes/prometheus/api_client/client.yml

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)