Skip to content

Commit 6fa32c8

Browse files
committed
[MODEL] Prevent index methods to swallow all exceptions
Closes elastic#303
1 parent f4df72f commit 6fa32c8

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,10 @@ def create_index!(options={})
187187
delete_index!(options.merge index: target_index) if options[:force]
188188

189189
unless ( self.client.indices.exists(index: target_index) rescue false )
190-
begin
191-
self.client.indices.create index: target_index,
192-
body: {
193-
settings: self.settings.to_hash,
194-
mappings: self.mappings.to_hash }
195-
rescue Exception => e
196-
unless e.class.to_s =~ /NotFound/ && options[:force]
197-
STDERR.puts "[!!!] Error when creating the index: #{e.class}", "#{e.message}"
198-
end
199-
end
200-
else
190+
self.client.indices.create index: target_index,
191+
body: {
192+
settings: self.settings.to_hash,
193+
mappings: self.mappings.to_hash }
201194
end
202195
end
203196

@@ -217,8 +210,10 @@ def delete_index!(options={})
217210
begin
218211
self.client.indices.delete index: target_index
219212
rescue Exception => e
220-
unless e.class.to_s =~ /NotFound/ && options[:force]
221-
STDERR.puts "[!!!] Error when deleting the index: #{e.class}", "#{e.message}"
213+
if e.class.to_s =~ /NotFound/ && options[:force]
214+
STDERR.puts "[!!!] Index does not exist (#{e.class})"
215+
else
216+
raise e
222217
end
223218
end
224219
end
@@ -241,8 +236,10 @@ def refresh_index!(options={})
241236
begin
242237
self.client.indices.refresh index: target_index
243238
rescue Exception => e
244-
unless e.class.to_s =~ /NotFound/ && options[:force]
245-
STDERR.puts "[!!!] Error when refreshing the index: #{e.class}", "#{e.message}"
239+
if e.class.to_s =~ /NotFound/ && options[:force]
240+
STDERR.puts "[!!!] Index does not exist (#{e.class})"
241+
else
242+
raise e
246243
end
247244
end
248245
end

elasticsearch-model/test/unit/indexing_test.rb

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def self.foo
1212
end
1313
end
1414

15+
class NotFound < Exception; end
16+
1517
context "Settings class" do
1618
should "be convertible to hash" do
1719
hash = { foo: 'bar' }
@@ -382,19 +384,40 @@ class ::DummyIndexingModelForRecreate
382384
end
383385
end
384386

385-
should "delete the index without raising exception" do
387+
should "delete the index without raising exception when the index is not found" do
386388
client = stub('client')
387389
indices = stub('indices')
388390
client.stubs(:indices).returns(indices)
389391

390-
indices.expects(:delete).returns({}).then.raises(Exception).at_least_once
392+
indices.expects(:delete).returns({}).then.raises(NotFound).at_least_once
391393

392394
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
393395

394-
assert_nothing_raised do
395-
DummyIndexingModelForRecreate.delete_index!
396-
DummyIndexingModelForRecreate.delete_index!
397-
end
396+
assert_nothing_raised { DummyIndexingModelForRecreate.delete_index! force: true }
397+
end
398+
399+
should "raise an exception without the force option" do
400+
client = stub('client')
401+
indices = stub('indices')
402+
client.stubs(:indices).returns(indices)
403+
404+
indices.expects(:delete).raises(NotFound)
405+
406+
DummyIndexingModelForRecreate.expects(:client).returns(client)
407+
408+
assert_raise { DummyIndexingModelForRecreate.delete_index! }
409+
end
410+
411+
should "raise a regular exception when deleting the index" do
412+
client = stub('client')
413+
indices = stub('indices')
414+
client.stubs(:indices).returns(indices)
415+
416+
indices.expects(:delete).raises(Exception)
417+
418+
DummyIndexingModelForRecreate.expects(:client).returns(client)
419+
420+
assert_raise { DummyIndexingModelForRecreate.delete_index! force: true }
398421
end
399422

400423
should "create the index with correct settings and mappings when it doesn't exist" do
@@ -430,19 +453,18 @@ class ::DummyIndexingModelForRecreate
430453
assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
431454
end
432455

433-
should "not raise exception during index creation" do
456+
should "raise exception during index creation" do
434457
client = stub('client')
435458
indices = stub('indices')
436459
client.stubs(:indices).returns(indices)
437460

461+
indices.expects(:delete).returns({})
438462
indices.expects(:exists).returns(false)
439-
indices.expects(:create).raises(Exception).at_least_once
463+
indices.expects(:create).raises(Exception)
440464

441465
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
442466

443-
assert_nothing_raised do
444-
DummyIndexingModelForRecreate.create_index!
445-
end
467+
assert_raise { DummyIndexingModelForRecreate.create_index! force: true }
446468
end
447469

448470
should "delete the index first with the force option" do
@@ -461,7 +483,19 @@ class ::DummyIndexingModelForRecreate
461483
end
462484
end
463485

464-
should "refresh the index without raising exception" do
486+
should "refresh the index without raising exception with the force option" do
487+
client = stub('client')
488+
indices = stub('indices')
489+
client.stubs(:indices).returns(indices)
490+
491+
indices.expects(:refresh).returns({}).then.raises(NotFound).at_least_once
492+
493+
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
494+
495+
assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
496+
end
497+
498+
should "raise a regular exception when refreshing the index" do
465499
client = stub('client')
466500
indices = stub('indices')
467501
client.stubs(:indices).returns(indices)
@@ -470,10 +504,7 @@ class ::DummyIndexingModelForRecreate
470504

471505
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
472506

473-
assert_nothing_raised do
474-
DummyIndexingModelForRecreate.refresh_index!
475-
DummyIndexingModelForRecreate.refresh_index!
476-
end
507+
assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
477508
end
478509

479510
context "with a custom index name" do

0 commit comments

Comments
 (0)