diff --git a/clients/algoliasearch-client-ruby/lib/algolia/error.rb b/clients/algoliasearch-client-ruby/lib/algolia/error.rb index 85a7447e343..a2cc8658328 100644 --- a/clients/algoliasearch-client-ruby/lib/algolia/error.rb +++ b/clients/algoliasearch-client-ruby/lib/algolia/error.rb @@ -10,6 +10,15 @@ class AlgoliaError < StandardError # Used when hosts are unreachable # 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 end # An exception class raised when the REST API returns an error. 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 diff --git a/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb b/clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb index 61839f0c623..14c7e4169bb 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 = {}) + retry_errors = [] + @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 + retry_errors << {host: host.url, error: response.error} + else + return response + end end - raise Algolia::AlgoliaUnreachableHostError, "Unreachable hosts" + raise Algolia::AlgoliaUnreachableHostError.new("Unreachable hosts.", retry_errors) end private