Skip to content

fix(ruby): add more retry error details #3384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clients/algoliasearch-client-ruby/lib/algolia/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions clients/algoliasearch-client-ruby/lib/algolia/logger_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading