Skip to content

Commit b0fa5a9

Browse files
otomatikThomas Dalousmillotp
authored
fix(ruby): add more retry error details (#3384)
Co-authored-by: Thomas Dalous <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent b8081e4 commit b0fa5a9

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clients/algoliasearch-client-ruby/lib/algolia/error.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ class AlgoliaError < StandardError
1010
# Used when hosts are unreachable
1111
#
1212
class AlgoliaUnreachableHostError < AlgoliaError
13+
attr_reader :errors
14+
15+
def initialize(message, errors = [])
16+
errors.last&.tap do |last_error|
17+
message += " Last error for #{last_error[:host]}: #{last_error[:error]}"
18+
end
19+
super(message)
20+
@errors = errors
21+
end
1322
end
1423

1524
# An exception class raised when the REST API returns an error.

clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ class LoggerHelper
55
# @param debug_file [nil|String] file used to output the logs
66
#
77
def self.create(debug_file = nil)
8-
file = debug_file || (ENV["ALGOLIA_DEBUG"] ? File.new("debug.log", "a+") : $stderr)
9-
instance = ::Logger.new(file)
8+
file = debug_file
9+
10+
if file.nil? && ENV["ALGOLIA_DEBUG"]
11+
begin
12+
file = File.new("debug.log", "a+")
13+
rescue Errno::EACCES, Errno::ENOENT => e
14+
puts "Failed to open debug.log: #{e.message}. Falling back to $stderr."
15+
end
16+
end
17+
18+
instance = ::Logger.new(file || $stderr)
1019
instance.progname = "algolia"
1120
instance
1221
end

clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def initialize(config, requester)
3939
# @return [Response] response of the request
4040
#
4141
def request(call_type, method, path, body, opts = {})
42+
retry_errors = []
43+
4244
@retry_strategy.get_tryable_hosts(call_type).each do |host|
4345
opts[:timeout] ||= get_timeout(call_type) * (host.retry_count + 1)
4446
opts[:connect_timeout] ||= @config.connect_timeout * (host.retry_count + 1)
@@ -71,10 +73,14 @@ def request(call_type, method, path, body, opts = {})
7173
raise Algolia::AlgoliaHttpError.new(response.status, decoded_error[:message])
7274
end
7375

74-
return response unless outcome == RETRY
76+
if outcome == RETRY
77+
retry_errors << {host: host.url, error: response.error}
78+
else
79+
return response
80+
end
7581
end
7682

77-
raise Algolia::AlgoliaUnreachableHostError, "Unreachable hosts"
83+
raise Algolia::AlgoliaUnreachableHostError.new("Unreachable hosts.", retry_errors)
7884
end
7985

8086
private

0 commit comments

Comments
 (0)