Skip to content

Custom error that includes the response on JSON parse error #44

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

Merged
merged 8 commits into from
Apr 23, 2025
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
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require: rubocop-rspec
plugins: rubocop-rspec

AllCops:
NewCops: enable
Expand Down Expand Up @@ -48,4 +48,4 @@ RSpec/MultipleExpectations:
Enabled: false

Style/HashSyntax:
Enabled: false # We still want to support older versions of Ruby
Enabled: false # We still want to support older versions of Ruby
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gem 'rspec', '~> 3.9'
gem 'rspec_junit_formatter', '~> 0.4'
gem 'rspec-legacy_formatters', '~> 1.0' # For codecov formatter
gem 'rubocop', '~> 1.12'
gem 'rubocop-rspec', '~> 2.4', require: false
gem 'rubocop-rspec', '~> 3.6', require: false
gem 'simplecov', '~> 0.18'
gem 'timecop', '~> 0.9'
gem 'webmock', '~> 3.8'
Expand Down
6 changes: 3 additions & 3 deletions lib/typesense/api_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def perform_request(method, endpoint, query_parameters: nil, body_parameters: ni
req.body = body
end
end
set_node_healthcheck(node, is_healthy: true) if response.status >= 1 && response.status <= 499
set_node_healthcheck(node, is_healthy: true) if response.status.between?(1, 499)

@logger.debug "Request #{method}:#{uri_for(endpoint, node)} to Node #{node[:index]} was successfully made (at the network layer). response.status was #{response.status}."

Expand All @@ -96,7 +96,7 @@ def perform_request(method, endpoint, query_parameters: nil, body_parameters: ni
end

# If response is 2xx return the object, else raise the response as an exception
return parsed_response if response.status >= 200 && response.status <= 299
return parsed_response if response.status.between?(200, 299)

exception_message = (parsed_response && parsed_response['message']) || 'Error'
raise custom_exception_klass_for(response), exception_message
Expand Down Expand Up @@ -190,7 +190,7 @@ def custom_exception_klass_for(response)
Typesense::Error::ObjectAlreadyExists.new(response: response)
elsif response.status == 422
Typesense::Error::ObjectUnprocessable.new(response: response)
elsif response.status >= 500 && response.status <= 599
elsif response.status.between?(500, 599)
Typesense::Error::ServerError.new(response: response)
elsif response.respond_to?(:timed_out?) && response.timed_out?
Typesense::Error::TimeoutError.new(response: response)
Expand Down
11 changes: 10 additions & 1 deletion lib/typesense/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ def import(documents, options = {})
)

if documents.is_a?(Array)
results_in_jsonl_format.split("\n").map { |r| Oj.load(r) }
results_in_jsonl_format.split("\n").map do |r|
Oj.load(r)
rescue Oj::ParseError => e
{
'success' => false,
'exception' => e.class.name,
'error' => e.message,
'json' => r
}
end
else
results_in_jsonl_format
end
Expand Down
12 changes: 6 additions & 6 deletions spec/typesense/api_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@
end

describe '#post' do
include_examples 'General error handling', :post
include_examples 'Node selection', :post
it_behaves_like 'General error handling', :post
it_behaves_like 'Node selection', :post
end

describe '#get' do
include_examples 'General error handling', :get
include_examples 'Node selection', :get
it_behaves_like 'General error handling', :get
it_behaves_like 'Node selection', :get
end

describe '#delete' do
include_examples 'General error handling', :delete
include_examples 'Node selection', :delete
it_behaves_like 'General error handling', :delete
it_behaves_like 'Node selection', :delete
end
end