From f6d02d8ed23c84393ab5140870795798760fca25 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Fri, 11 Aug 2023 15:55:38 +0200 Subject: [PATCH 1/7] Custom error that includes the response on JSON parse error --- lib/typesense/documents.rb | 8 +++++++- lib/typesense/error.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/typesense/documents.rb b/lib/typesense/documents.rb index c59ca84..35efbc3 100644 --- a/lib/typesense/documents.rb +++ b/lib/typesense/documents.rb @@ -46,7 +46,7 @@ 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 { |r| json_load(r) } else results_in_jsonl_format end @@ -73,5 +73,11 @@ def delete(query_parameters = {}) def endpoint_path(operation = nil) "#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}" end + + def json_load(json) + Oj.load(json) + rescue Oj::ParseError => e + raise Typesense::Error::ResponseMalformed, "#{e.message}\nJSON:\n#{json}" + end end end diff --git a/lib/typesense/error.rb b/lib/typesense/error.rb index f201be8..0074e53 100644 --- a/lib/typesense/error.rb +++ b/lib/typesense/error.rb @@ -25,6 +25,9 @@ class ObjectUnprocessable < Error class RequestMalformed < Error end + class ResponseMalformed < Error + end + class RequestUnauthorized < Error end From f4ecacd8a6b4c0657edb59588e1737ff7715b3c9 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Sat, 12 Aug 2023 10:18:24 +0200 Subject: [PATCH 2/7] Full JSON response --- lib/typesense/documents.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/typesense/documents.rb b/lib/typesense/documents.rb index 35efbc3..c8d3692 100644 --- a/lib/typesense/documents.rb +++ b/lib/typesense/documents.rb @@ -46,7 +46,16 @@ def import(documents, options = {}) ) if documents.is_a?(Array) - results_in_jsonl_format.split("\n").map { |r| json_load(r) } + results_in_jsonl_format.split("\n").map do |r| + begin + Oj.load(r) + rescue Oj::ParseError => e + raise Typesense::Error::ResponseMalformed, + "#{e.message}\n\n" \ + "JSON:\n#{r}\n\n" \ + "Full JSON:\n#{results_in_jsonl_format}" + end + end else results_in_jsonl_format end @@ -73,11 +82,5 @@ def delete(query_parameters = {}) def endpoint_path(operation = nil) "#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}" end - - def json_load(json) - Oj.load(json) - rescue Oj::ParseError => e - raise Typesense::Error::ResponseMalformed, "#{e.message}\nJSON:\n#{json}" - end end end From eb5518c68a3439864c050b861061b0dea14417b7 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 15 Aug 2023 15:37:20 +0200 Subject: [PATCH 3/7] Replace exception with simpler JSON --- lib/typesense/documents.rb | 10 ++++++---- lib/typesense/error.rb | 3 --- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/typesense/documents.rb b/lib/typesense/documents.rb index c8d3692..699f55a 100644 --- a/lib/typesense/documents.rb +++ b/lib/typesense/documents.rb @@ -50,10 +50,12 @@ def import(documents, options = {}) begin Oj.load(r) rescue Oj::ParseError => e - raise Typesense::Error::ResponseMalformed, - "#{e.message}\n\n" \ - "JSON:\n#{r}\n\n" \ - "Full JSON:\n#{results_in_jsonl_format}" + { + "success" => false, + "exception" => e.class.name, + "error" => e.message, + "json" => r, + } end end else diff --git a/lib/typesense/error.rb b/lib/typesense/error.rb index 0074e53..f201be8 100644 --- a/lib/typesense/error.rb +++ b/lib/typesense/error.rb @@ -25,9 +25,6 @@ class ObjectUnprocessable < Error class RequestMalformed < Error end - class ResponseMalformed < Error - end - class RequestUnauthorized < Error end From aaa7819ff66616966ef727859e834887145cdac7 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 22 Apr 2025 16:18:41 +0200 Subject: [PATCH 4/7] Lint fix --- lib/typesense/documents.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/typesense/documents.rb b/lib/typesense/documents.rb index 8aeffbe..a5423f7 100644 --- a/lib/typesense/documents.rb +++ b/lib/typesense/documents.rb @@ -51,16 +51,14 @@ def import(documents, options = {}) if documents.is_a?(Array) results_in_jsonl_format.split("\n").map do |r| - begin - Oj.load(r) - rescue Oj::ParseError => e - { - "success" => false, - "exception" => e.class.name, - "error" => e.message, - "json" => r, - } - end + Oj.load(r) + rescue Oj::ParseError => e + { + 'success' => false, + 'exception' => e.class.name, + 'error' => e.message, + 'json' => r + } end else results_in_jsonl_format From 513a23fb54fbb4c1a9dfd219bc78b87e40b67738 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 22 Apr 2025 16:20:10 +0200 Subject: [PATCH 5/7] Lint fix --- lib/typesense/api_call.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/typesense/api_call.rb b/lib/typesense/api_call.rb index 19a3f47..5812667 100644 --- a/lib/typesense/api_call.rb +++ b/lib/typesense/api_call.rb @@ -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}." @@ -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 @@ -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) From 6e26e4be17440a59aee08392a777c3b7a0b1fc5a Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 22 Apr 2025 18:01:52 +0200 Subject: [PATCH 6/7] Bum rubocop-rspec --- .rubocop.yml | 2 +- Gemfile | 2 +- spec/typesense/api_call_spec.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 55a805b..6c708dd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -48,4 +48,4 @@ RSpec/MultipleExpectations: Enabled: false Style/HashSyntax: - Enabled: false # We still want to support older versions of Ruby \ No newline at end of file + Enabled: false # We still want to support older versions of Ruby diff --git a/Gemfile b/Gemfile index b18290a..5337260 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/spec/typesense/api_call_spec.rb b/spec/typesense/api_call_spec.rb index dbe22aa..5e35313 100644 --- a/spec/typesense/api_call_spec.rb +++ b/spec/typesense/api_call_spec.rb @@ -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 From 638ad86bc1a484a11806abd55e4ae25c32b9c1a6 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 22 Apr 2025 18:04:19 +0200 Subject: [PATCH 7/7] Plugins --- .rubocop.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6c708dd..64c0c3e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,4 @@ -require: rubocop-rspec +plugins: rubocop-rspec AllCops: NewCops: enable