Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ class SafeIndex
def initialize(index_uid, raise_on_failure, options)
client = MeiliSearch::Rails.client
primary_key = options[:primary_key] || MeiliSearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
client.create_index(index_uid, { primaryKey: primary_key })
@index = client.index(index_uid)
@raise_on_failure = raise_on_failure.nil? || raise_on_failure

SafeIndex.log_or_throw(nil, @raise_on_failure) do
client.create_index(index_uid, { primaryKey: primary_key })
end

@index = client.index(index_uid)
end

::MeiliSearch::Index.instance_methods(false).each do |m|
Expand Down Expand Up @@ -273,7 +277,7 @@ def settings(*args)

def self.log_or_throw(method, raise_on_failure, &block)
yield
rescue ::MeiliSearch::ApiError => e
rescue ::MeiliSearch::TimeoutError, ::MeiliSearch::ApiError => e
raise e if raise_on_failure

# log the error
Expand Down
29 changes: 29 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ class SerializedDocument < ActiveRecord::Base
end

describe 'Raise on failure' do
before { Vegetable.instance_variable_set('@ms_indexes', nil) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required because I'm messing with the internals of that class when I'm adding/removing the mocks to raise the TimeoutError.


it 'raises on failure' do
expect do
Fruit.search('', { filter: 'title = Nightshift' })
Expand All @@ -1410,6 +1412,33 @@ class SerializedDocument < ActiveRecord::Base
Vegetable.search('', { filter: 'title = Kale' })
end.not_to raise_error
end

context 'when Meilisearch server take too long to answer' do
let(:index_instance) { instance_double(MeiliSearch::Index, settings: nil, update_settings: nil) }
let(:slow_client) { instance_double(MeiliSearch::Client, index: index_instance) }

it 'does not raise error timeouts on reindex' do
allow(index_instance).to receive(:add_documents).and_raise(MeiliSearch::TimeoutError)
allow(slow_client).to receive(:create_index).and_return(index_instance)

allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)

expect do
Vegetable.create(name: 'potato')
end.not_to raise_error
end

it 'does not raise error timeouts on data addition' do
allow(slow_client).to receive(:create_index).and_raise(MeiliSearch::TimeoutError)
allow(index_instance).to receive(:add_documents).and_return(nil)

allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)

expect do
Vegetable.ms_reindex!
end.not_to raise_error
end
end
end

context "when have a internal class defined in the app's scope" do
Expand Down