Skip to content

Commit 9848c97

Browse files
aeroastrokarmi
authored andcommitted
[CLIENT] Swallow logging of exceptions when the ignore is specified
The primary objective of this PR is to swallow fatal log in addition to exception, which has been supported since 6ae36e4. In my humble opinion, when HTTP status code is explicitly specified with ignore option, receiving the status code is not fatal. For example, [404] fatal log invoked by checking document existence by HEAD request is not so informative nor required, I think. Closes #415
1 parent aba2492 commit 9848c97

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ def perform_request(method, path, params={}, body=nil, &block)
313313
if response.status.to_i >= 300
314314
__log method, path, params, body, url, response, nil, 'N/A', duration if logger
315315
__trace method, path, params, body, url, response, nil, 'N/A', duration if tracer
316-
__log_failed response if logger
317316

318-
# Swallow the exception when the `ignore` parameter matches response status
317+
# Swallow the exception and log when the `ignore` parameter matches response status
318+
__log_failed response if logger && !ignore.include?(response.status.to_i)
319319
__raise_transport_error response unless ignore.include?(response.status.to_i)
320320
end
321321

elasticsearch-transport/test/unit/transport_base_test.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def initialize(*); end
420420
@transport.perform_request('GET', '/') {Elasticsearch::Transport::Transport::Response.new 200, '{"foo":"bar"}' }
421421
end
422422

423-
should "log a failed Elasticsearch request" do
423+
should "log a failed Elasticsearch request as fatal" do
424424
@block = Proc.new { |c, u| puts "ERROR" }
425425
@block.expects(:call).returns(Elasticsearch::Transport::Transport::Response.new 500, 'ERROR')
426426

@@ -432,6 +432,17 @@ def initialize(*); end
432432
end
433433
end unless RUBY_1_8
434434

435+
should "not log a failed Elasticsearch request as fatal" do
436+
@block = Proc.new { |c, u| puts "ERROR" }
437+
@block.expects(:call).returns(Elasticsearch::Transport::Transport::Response.new 500, 'ERROR')
438+
439+
@transport.expects(:__log).twice
440+
@transport.logger.expects(:fatal).never
441+
442+
# No `BadRequest` error
443+
@transport.perform_request('POST', '_search', :ignore => 500, &@block)
444+
end unless RUBY_1_8
445+
435446
should "log and re-raise a Ruby exception" do
436447
@block = Proc.new { |c, u| puts "ERROR" }
437448
@block.expects(:call).raises(Exception)

0 commit comments

Comments
 (0)