Skip to content

Commit

Permalink
Test version_error_handler with realistic data
Browse files Browse the repository at this point in the history
Meilisearch::ApiError parses a server's JSON response into its
`@http_body` instance variable which stores a hash.
When `version_error_handler` propagates ApiError, it passes `@http_body`
as the server http body which causes ApiError to attempt to parse it
again as JSON, throwing a TypeError.
  • Loading branch information
ellnix committed Jan 17, 2024
1 parent 40f80cb commit 6e091ad
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions spec/meilisearch/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,33 @@
end

describe '.version_error_handler' do
let(:http_body) do
{ 'message' => 'Was expecting an operation',
'code' => 'invalid_document_filter',
'type' => 'invalid_request',
'link' => 'https://docs.meilisearch.com/errors#invalid_document_filter' }
end

it 'spawns same error message' do
expect do
described_class.version_error_handler(:my_method) do
raise MeiliSearch::ApiError.new(405, 'I came from Meili server', {})
raise MeiliSearch::ApiError.new(405, 'I came from Meili server', http_body)
end
end.to raise_error(MeiliSearch::ApiError, /I came from Meili server/)
end

it 'spawns same error message with no body' do
expect do
described_class.version_error_handler(:my_method) do
raise MeiliSearch::ApiError.new(405, 'I came from Meili server', nil)
end
end.to raise_error(MeiliSearch::ApiError, /I came from Meili server/)
end

it 'spawns message with version hint' do
expect do
described_class.version_error_handler(:my_method) do
raise MeiliSearch::ApiError.new(405, 'I came from Meili server', {})
raise MeiliSearch::ApiError.new(405, 'I came from Meili server', http_body)
end
end.to raise_error(MeiliSearch::ApiError, /that `my_method` call requires/)
end
Expand Down

0 comments on commit 6e091ad

Please sign in to comment.