From d1b3dd9cdb8f3d49f0e7f87e7e81873d3032d226 Mon Sep 17 00:00:00 2001 From: Thomas Dalous Date: Thu, 18 Jul 2024 11:13:39 +0200 Subject: [PATCH 1/4] fix(error): add more retry error details --- .../lib/algolia/transport/transport.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb index 61839f0c623..8e213f8726b 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb @@ -39,6 +39,8 @@ def initialize(config, requester) # @return [Response] response of the request # def request(call_type, method, path, body, opts = {}) + last_retry = {host: nil, error: nil} + @retry_strategy.get_tryable_hosts(call_type).each do |host| opts[:timeout] ||= get_timeout(call_type) * (host.retry_count + 1) opts[:connect_timeout] ||= @config.connect_timeout * (host.retry_count + 1) @@ -71,10 +73,14 @@ def request(call_type, method, path, body, opts = {}) raise Algolia::AlgoliaHttpError.new(response.status, decoded_error[:message]) end - return response unless outcome == RETRY + if outcome == RETRY + last_retry = {host: host.url, error: response.error} + else + return response + end end - raise Algolia::AlgoliaUnreachableHostError, "Unreachable hosts" + raise Algolia::AlgoliaUnreachableHostError, "Unreachable hosts. Last error for #{last_retry[:host]}: #{last_retry[:error]}" end private From e84f5e5f9e587e94a8c427fc8ad9ce4c62b81ff9 Mon Sep 17 00:00:00 2001 From: Thomas Dalous Date: Fri, 19 Jul 2024 14:07:06 +0200 Subject: [PATCH 2/4] fix(error): add list of retry errors --- clients/algoliasearch-client-ruby/lib/algolia/error.rb | 6 ++++++ .../lib/algolia/transport/transport.rb | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/clients/algoliasearch-client-ruby/lib/algolia/error.rb b/clients/algoliasearch-client-ruby/lib/algolia/error.rb index 85a7447e343..fcf380e3feb 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/error.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/error.rb @@ -10,6 +10,12 @@ class AlgoliaError < StandardError # Used when hosts are unreachable # class AlgoliaUnreachableHostError < AlgoliaError + attr_reader :errors + + def initialize(message, errors = []) + super(message) + @errors = errors + end end # An exception class raised when the REST API returns an error. diff --git a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb index 8e213f8726b..ad5fc0253ef 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb @@ -39,7 +39,7 @@ def initialize(config, requester) # @return [Response] response of the request # def request(call_type, method, path, body, opts = {}) - last_retry = {host: nil, error: nil} + retry_errors = [] @retry_strategy.get_tryable_hosts(call_type).each do |host| opts[:timeout] ||= get_timeout(call_type) * (host.retry_count + 1) @@ -74,13 +74,13 @@ def request(call_type, method, path, body, opts = {}) end if outcome == RETRY - last_retry = {host: host.url, error: response.error} + retry_errors << {host: host.url, error: response.error} else return response end end - raise Algolia::AlgoliaUnreachableHostError, "Unreachable hosts. Last error for #{last_retry[:host]}: #{last_retry[:error]}" + raise Algolia::AlgoliaUnreachableHostError.new("Unreachable hosts. Last error for #{retry_errors.last[:host]}: #{retry_errors.last[:error]}", retry_errors) end private From d91ecc8b88a7d5da79ee596ba85f127bd47722d2 Mon Sep 17 00:00:00 2001 From: Thomas Dalous Date: Fri, 19 Jul 2024 14:07:53 +0200 Subject: [PATCH 3/4] fix(debug): make sure it fallbacks on stderr anyway --- .../lib/algolia/logger_helper.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb b/clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb index c968bf687b4..3652ae3d2cb 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb @@ -5,8 +5,17 @@ class LoggerHelper # @param debug_file [nil|String] file used to output the logs # def self.create(debug_file = nil) - file = debug_file || (ENV["ALGOLIA_DEBUG"] ? File.new("debug.log", "a+") : $stderr) - instance = ::Logger.new(file) + file = debug_file + + if file.nil? && ENV["ALGOLIA_DEBUG"] + begin + file = File.new("debug.log", "a+") + rescue Errno::EACCES, Errno::ENOENT => e + puts "Failed to open debug.log: #{e.message}. Falling back to $stderr." + end + end + + instance = ::Logger.new(file || $stderr) instance.progname = "algolia" instance end From 5bd28704ac87f377019fb326ccbf2f79709703f9 Mon Sep 17 00:00:00 2001 From: Thomas Dalous Date: Tue, 23 Jul 2024 13:47:07 +0200 Subject: [PATCH 4/4] chore: refacto --- clients/algoliasearch-client-ruby/lib/algolia/error.rb | 3 +++ .../lib/algolia/transport/transport.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-ruby/lib/algolia/error.rb b/clients/algoliasearch-client-ruby/lib/algolia/error.rb index fcf380e3feb..a2cc8658328 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/error.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/error.rb @@ -13,6 +13,9 @@ class AlgoliaUnreachableHostError < AlgoliaError attr_reader :errors def initialize(message, errors = []) + errors.last&.tap do |last_error| + message += " Last error for #{last_error[:host]}: #{last_error[:error]}" + end super(message) @errors = errors end diff --git a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb index ad5fc0253ef..14c7e4169bb 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb @@ -80,7 +80,7 @@ def request(call_type, method, path, body, opts = {}) end end - raise Algolia::AlgoliaUnreachableHostError.new("Unreachable hosts. Last error for #{retry_errors.last[:host]}: #{retry_errors.last[:error]}", retry_errors) + raise Algolia::AlgoliaUnreachableHostError.new("Unreachable hosts.", retry_errors) end private