Skip to content

Commit

Permalink
Merge #307
Browse files Browse the repository at this point in the history
307: Improve error handling, rescue EPIPE and Net::* errors r=brunoocasali a=brunoocasali

# Pull Request
This PR fixes a bad DX when comes to errors, if a user tries to upload a big dataset, they will face some errors, one of them was related to the `Errno::EPIPE` error, which is really hard to figure out.

The idea of this PR is to handle the errors and improve the user experience.

## What does this PR do?
Fixes #305 

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


- [ ] Try something with other lib, which address the pipes error better like: httprb/http#473

Co-authored-by: Bruno Casali <[email protected]>
  • Loading branch information
meili-bors[bot] and brunoocasali authored Apr 7, 2022
2 parents 0f545bd + 61aa4ad commit 8d5caef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/meilisearch/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def initialize(message)
class TimeoutError < StandardError
attr_reader :message

def initialize
@message = 'The update was not processed in the expected time'
def initialize(message = nil)
@message = "The request was not processed in the expected time. #{message}"
super(@message)
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ def send_request(http_method, relative_path, config: {})

begin
response = http_method.call(@base_url + relative_path, config)
rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EPIPE => e
raise CommunicationError, e.message
rescue Net::ReadTimeout, Net::OpenTimeout => e
raise TimeoutError, e.message
end

validate(response)
Expand Down
25 changes: 25 additions & 0 deletions spec/meilisearch/client/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Client - Errors' do
context 'when request takes to long to answer' do
it 'raises MeiliSearch::TimeoutError' do
timed_client = MeiliSearch::Client.new(URL, MASTER_KEY, { timeout: 0.000001 })

expect do
timed_client.version
end.to raise_error(MeiliSearch::TimeoutError)
end
end

context 'when body is too large' do
let(:index) { client.index('movies') }

it 'raises MeiliSearch::CommunicationError' do
allow(index.class).to receive(:post).and_raise(Errno::EPIPE)

expect do
index.add_documents([{ id: 1, text: 'my_text' }])
end.to raise_error(MeiliSearch::CommunicationError)
end
end
end
2 changes: 1 addition & 1 deletion spec/meilisearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

expect do
new_client.indexes
end.to raise_error(Timeout::Error)
end.to raise_error(MeiliSearch::TimeoutError)
end

it 'has a pre-defined header with current version' do
Expand Down

0 comments on commit 8d5caef

Please sign in to comment.