Skip to content

Commit

Permalink
fix(ruby): add more retry error details (#3384)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Dalous <[email protected]>
Co-authored-by: Pierre Millot <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent b8081e4 commit b0fa5a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit b0fa5a9

Please sign in to comment.