From 33b571b9be9d478074d7e4cfb69368e28c44e9b5 Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:12:23 +0200 Subject: [PATCH 1/4] Add MeiliSearch::Error and use as base error --- lib/meilisearch/error.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/meilisearch/error.rb b/lib/meilisearch/error.rb index 6f160ca0..7a9f7b61 100644 --- a/lib/meilisearch/error.rb +++ b/lib/meilisearch/error.rb @@ -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 @@ -45,7 +48,7 @@ def details end end - class CommunicationError < StandardError + class CommunicationError < Error attr_reader :message def initialize(message) @@ -54,7 +57,7 @@ def initialize(message) end end - class TimeoutError < StandardError + class TimeoutError < Error attr_reader :message def initialize(message = nil) From 2529dea4cb82e3977b4d84da7a5e5476857b5a4e Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:20:41 +0200 Subject: [PATCH 2/4] Add tests for new Error structure --- spec/meilisearch/client/errors_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/meilisearch/client/errors_spec.rb b/spec/meilisearch/client/errors_spec.rb index 2ec36c23..480482ca 100644 --- a/spec/meilisearch/client/errors_spec.rb +++ b/spec/meilisearch/client/errors_spec.rb @@ -1,6 +1,26 @@ # frozen_string_literal: true RSpec.describe 'MeiliSearch::Client - Errors' do + describe 'MeiliSearch::Error' do + it 'catches MeiliSearch::TimeoutError' do + expect do + raise MeiliSearch::TimeoutError + end.to raise_error(MeiliSearch::Error) + end + + it 'catches MeiliSearch::CommunicationError' do + expect do + raise MeiliSearch::CommunicationError, '' + end.to raise_error(MeiliSearch::Error) + end + + it 'catches MeiliSearch::ApiError' do + expect do + raise MeiliSearch::ApiError.new(200, '', '') + end.to raise_error(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 }) From a246782fceaf4cb2ff1cabd06b22403775370ff6 Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:19:47 +0200 Subject: [PATCH 3/4] Update error inherintance testing logic --- spec/meilisearch/client/errors_spec.rb | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/spec/meilisearch/client/errors_spec.rb b/spec/meilisearch/client/errors_spec.rb index 480482ca..9680e939 100644 --- a/spec/meilisearch/client/errors_spec.rb +++ b/spec/meilisearch/client/errors_spec.rb @@ -2,22 +2,10 @@ RSpec.describe 'MeiliSearch::Client - Errors' do describe 'MeiliSearch::Error' do - it 'catches MeiliSearch::TimeoutError' do - expect do - raise MeiliSearch::TimeoutError - end.to raise_error(MeiliSearch::Error) - end - - it 'catches MeiliSearch::CommunicationError' do - expect do - raise MeiliSearch::CommunicationError, '' - end.to raise_error(MeiliSearch::Error) - end - - it 'catches MeiliSearch::ApiError' do - expect do - raise MeiliSearch::ApiError.new(200, '', '') - end.to raise_error(MeiliSearch::Error) + 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) end end From dc3f9bde5c774ede64ed8fc663fa647391bb63aa Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Mon, 9 Oct 2023 14:26:52 +0200 Subject: [PATCH 4/4] Add TenantToken errors as well --- lib/meilisearch/error.rb | 6 +++--- spec/meilisearch/client/errors_spec.rb | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/meilisearch/error.rb b/lib/meilisearch/error.rb index 7a9f7b61..01d3776f 100644 --- a/lib/meilisearch/error.rb +++ b/lib/meilisearch/error.rb @@ -67,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 diff --git a/spec/meilisearch/client/errors_spec.rb b/spec/meilisearch/client/errors_spec.rb index 9680e939..10843cf4 100644 --- a/spec/meilisearch/client/errors_spec.rb +++ b/spec/meilisearch/client/errors_spec.rb @@ -6,6 +6,9 @@ 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