Skip to content
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

fix(retry): better error #501

Open
wants to merge 9 commits into
base: v2stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion lib/algolia/logger_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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') : $stderr)
file = debug_file || (ENV['ALGOLIA_DEBUG'] ? File.open('debug.log', 'a+') : $stderr)
instance = ::Logger.new file
instance.progname = 'algolia'
instance
Expand Down
2 changes: 1 addition & 1 deletion lib/algolia/transport/retry_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Algolia
module Transport
# Class RetryStatregy
# Class RetryStrategy
class RetryStrategy
include RetryOutcomeType

Expand Down
10 changes: 8 additions & 2 deletions lib/algolia/transport/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def write(method, path, body = {}, opts = {})
# @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)
Expand All @@ -73,10 +75,14 @@ def request(call_type, method, path, body = {}, opts = {})
decoded_error = json_to_hash(response.error, @config.symbolize_keys)
raise AlgoliaHttpError.new(get_option(decoded_error, 'status'), get_option(decoded_error, 'message'))
end
return json_to_hash(response.body, @config.symbolize_keys) unless outcome == RETRY
if outcome == RETRY
last_retry = {host: host.url, error: response.error}
else
return json_to_hash(response.body, @config.symbolize_keys)
end
end

raise AlgoliaUnreachableHostError, 'Unreachable hosts'
raise AlgoliaUnreachableHostError, "Unreachable hosts. Last error for #{last_retry[:host]}: #{last_retry[:error]}"
end

private
Expand Down
4 changes: 2 additions & 2 deletions test/algolia/unit/retry_strategy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def test_failure_when_all_hosts_are_down
index.save_object({ objectID: 'one' })
end

assert_equal 'Unreachable hosts', exception.message
assert_includes exception.message, 'Unreachable hosts. Last error for 0.0.0.0: SSL_connect'
end
end

describe 'retry stategy decisions' do
describe 'retry strategy decisions' do
def before_all
super
@app_id = 'app_id'
Expand Down