Skip to content

Commit 6ae36e4

Browse files
committed
[CLIENT] Added support for the ignore parameter in all API calls
1 parent 9e8456e commit 6ae36e4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ def perform_request(method, path, params={}, body=nil, &block)
244244
start = Time.now if logger || tracer
245245
tries = 0
246246

247+
params = params.clone
248+
249+
ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }
250+
247251
begin
248252
tries += 1
249253
connection = get_connection or raise Error.new("Cannot get new connection from pool.")
@@ -309,7 +313,9 @@ def perform_request(method, path, params={}, body=nil, &block)
309313
__log method, path, params, body, url, response, nil, 'N/A', duration if logger
310314
__trace method, path, params, body, url, response, nil, 'N/A', duration if tracer
311315
__log_failed response if logger
312-
__raise_transport_error response
316+
317+
# Swallow the exception when the `ignore` parameter matches response status
318+
__raise_transport_error response unless ignore.include?(response.status.to_i)
313319
end
314320

315321
json = serializer.load(response.body) if response.body && !response.body.empty? && response.headers && response.headers["content-type"] =~ /json/

elasticsearch-transport/test/integration/client_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
5353
end
5454
end
5555

56+
should "ignore specified response codes" do
57+
response = @client.perform_request 'GET', '/_foobar', ignore: 400
58+
assert_equal 400, response.status
59+
60+
assert_instance_of Hash, response.body
61+
assert_match /illegal_argument_exception/, response.body.inspect
62+
end
63+
5664
should "pass options to the transport" do
5765
@client = Elasticsearch::Client.new \
5866
host: "127.0.0.1:#{@port}",

elasticsearch-transport/test/unit/transport_base_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ def initialize(*); end
246246
end
247247
end
248248

249+
should "not raise an error when the :ignore argument has been passed" do
250+
@transport.stubs(:get_connection).returns(stub_everything :failures => 1)
251+
252+
assert_raise Elasticsearch::Transport::Transport::Errors::BadRequest do
253+
@transport.perform_request 'GET', '/' do
254+
Elasticsearch::Transport::Transport::Response.new 400, 'CLIENT ERROR'
255+
end
256+
end
257+
258+
# No `BadRequest` error
259+
@transport.perform_request 'GET', '/', :ignore => 400 do
260+
Elasticsearch::Transport::Transport::Response.new 400, 'CLIENT ERROR'
261+
end
262+
end
263+
249264
should "mark the connection as dead on failure" do
250265
c = stub_everything :failures => 1
251266
@transport.expects(:get_connection).returns(c)

0 commit comments

Comments
 (0)