Skip to content

Commit 2cf966b

Browse files
authored
Merge pull request #44 from cults/response-malformed-json-error
Custom error that includes the response on JSON parse error
2 parents 9240cda + 638ad86 commit 2cf966b

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

.rubocop.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require: rubocop-rspec
1+
plugins: rubocop-rspec
22

33
AllCops:
44
NewCops: enable
@@ -48,4 +48,4 @@ RSpec/MultipleExpectations:
4848
Enabled: false
4949

5050
Style/HashSyntax:
51-
Enabled: false # We still want to support older versions of Ruby
51+
Enabled: false # We still want to support older versions of Ruby

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gem 'rspec', '~> 3.9'
1616
gem 'rspec_junit_formatter', '~> 0.4'
1717
gem 'rspec-legacy_formatters', '~> 1.0' # For codecov formatter
1818
gem 'rubocop', '~> 1.12'
19-
gem 'rubocop-rspec', '~> 2.4', require: false
19+
gem 'rubocop-rspec', '~> 3.6', require: false
2020
gem 'simplecov', '~> 0.18'
2121
gem 'timecop', '~> 0.9'
2222
gem 'webmock', '~> 3.8'

lib/typesense/api_call.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def perform_request(method, endpoint, query_parameters: nil, body_parameters: ni
8585
req.body = body
8686
end
8787
end
88-
set_node_healthcheck(node, is_healthy: true) if response.status >= 1 && response.status <= 499
88+
set_node_healthcheck(node, is_healthy: true) if response.status.between?(1, 499)
8989

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

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

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

101101
exception_message = (parsed_response && parsed_response['message']) || 'Error'
102102
raise custom_exception_klass_for(response), exception_message
@@ -190,7 +190,7 @@ def custom_exception_klass_for(response)
190190
Typesense::Error::ObjectAlreadyExists.new(response: response)
191191
elsif response.status == 422
192192
Typesense::Error::ObjectUnprocessable.new(response: response)
193-
elsif response.status >= 500 && response.status <= 599
193+
elsif response.status.between?(500, 599)
194194
Typesense::Error::ServerError.new(response: response)
195195
elsif response.respond_to?(:timed_out?) && response.timed_out?
196196
Typesense::Error::TimeoutError.new(response: response)

lib/typesense/documents.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ def import(documents, options = {})
5050
)
5151

5252
if documents.is_a?(Array)
53-
results_in_jsonl_format.split("\n").map { |r| Oj.load(r) }
53+
results_in_jsonl_format.split("\n").map do |r|
54+
Oj.load(r)
55+
rescue Oj::ParseError => e
56+
{
57+
'success' => false,
58+
'exception' => e.class.name,
59+
'error' => e.message,
60+
'json' => r
61+
}
62+
end
5463
else
5564
results_in_jsonl_format
5665
end

spec/typesense/api_call_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,17 @@
245245
end
246246

247247
describe '#post' do
248-
include_examples 'General error handling', :post
249-
include_examples 'Node selection', :post
248+
it_behaves_like 'General error handling', :post
249+
it_behaves_like 'Node selection', :post
250250
end
251251

252252
describe '#get' do
253-
include_examples 'General error handling', :get
254-
include_examples 'Node selection', :get
253+
it_behaves_like 'General error handling', :get
254+
it_behaves_like 'Node selection', :get
255255
end
256256

257257
describe '#delete' do
258-
include_examples 'General error handling', :delete
259-
include_examples 'Node selection', :delete
258+
it_behaves_like 'General error handling', :delete
259+
it_behaves_like 'Node selection', :delete
260260
end
261261
end

0 commit comments

Comments
 (0)