Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new MeiliSearch::Error from which other errors inherit #492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions lib/meilisearch/error.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

module MeiliSearch
class ApiError < StandardError
class Error < StandardError
end

class ApiError < Error
# :http_code # e.g. 400, 404...
# :http_message # e.g. Bad Request, Not Found...
# :http_body # The response body received from the MeiliSearch API
Expand Down Expand Up @@ -45,7 +48,7 @@ def details
end
end

class CommunicationError < StandardError
class CommunicationError < Error
attr_reader :message

def initialize(message)
Expand All @@ -54,7 +57,7 @@ def initialize(message)
end
end

class TimeoutError < StandardError
class TimeoutError < Error
attr_reader :message

def initialize(message = nil)
Expand All @@ -64,8 +67,8 @@ def initialize(message = nil)
end

module TenantToken
class ExpireOrInvalidSignature < StandardError; end
class InvalidApiKey < StandardError; end
class InvalidSearchRules < StandardError; end
class ExpireOrInvalidSignature < MeiliSearch::Error; end
class InvalidApiKey < MeiliSearch::Error; end
class InvalidSearchRules < MeiliSearch::Error; end
end
end
11 changes: 11 additions & 0 deletions spec/meilisearch/client/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Client - Errors' do
describe 'MeiliSearch::Error' do
it 'catches all other errors' do
expect(MeiliSearch::TimeoutError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::CommunicationError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::ApiError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::InvalidApiKey.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::InvalidSearchRules.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::ExpireOrInvalidSignature.ancestors).to include(MeiliSearch::Error)
end
end

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 })
Expand Down