Skip to content

Commit 337d639

Browse files
author
Thomas Dalous
committed
fix(requests): keep track of all retry errors
1 parent 813f632 commit 337d639

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/algolia/error.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ 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+
super(message)
17+
@errors = errors
18+
end
1319
end
1420

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

lib/algolia/transport/transport.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def write(method, path, body = {}, opts = {})
4949
# @return [Response] response of the request
5050
#
5151
def request(call_type, method, path, body = {}, opts = {})
52-
last_retry = {host: nil, error: nil}
52+
retry_errors = []
5353

5454
@retry_strategy.get_tryable_hosts(call_type).each do |host|
5555
opts[:timeout] ||= get_timeout(call_type) * (host.retry_count + 1)
@@ -76,13 +76,13 @@ def request(call_type, method, path, body = {}, opts = {})
7676
raise AlgoliaHttpError.new(get_option(decoded_error, 'status'), get_option(decoded_error, 'message'))
7777
end
7878
if outcome == RETRY
79-
last_retry = {host: host.url, error: response.error}
79+
retry_errors << {host: host.url, error: response.error}
8080
else
8181
return json_to_hash(response.body, @config.symbolize_keys)
8282
end
8383
end
8484

85-
raise AlgoliaUnreachableHostError, "Unreachable hosts. Last error for #{last_retry[:host]}: #{last_retry[:error]}"
85+
raise AlgoliaUnreachableHostError.new("Unreachable hosts. Last error for #{retry_errors.last[:host]}: #{retry_errors.last[:error]}", retry_errors)
8686
end
8787

8888
private

test/algolia/unit/retry_strategy_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_resets_all_hosts_when_expired_according_to_write_type
7373

7474
describe 'All hosts are unreachable' do
7575
def test_failure_when_all_hosts_are_down
76-
stateful_hosts = ['0.0.0.0']
76+
stateful_hosts = ['0.0.0.0', '1.0.0.0']
7777
@config = Algolia::Search::Config.new(application_id: 'foo', api_key: 'bar', custom_hosts: stateful_hosts)
7878
client = Algolia::Search::Client.create_with_config(@config)
7979
index = client.init_index(get_test_index_name('failure'))
@@ -82,7 +82,11 @@ def test_failure_when_all_hosts_are_down
8282
index.save_object({ objectID: 'one' })
8383
end
8484

85-
assert_includes exception.message, 'Unreachable hosts. Last error for 0.0.0.0: SSL_connect'
85+
assert_includes exception.message, 'Unreachable hosts. Last error for 1.0.0.0: SSL_connect'
86+
assert_equal exception.errors, [
87+
{:host=>"0.0.0.0", :error=>"SSL_connect SYSCALL returned=5 errno=0 peeraddr=127.0.0.1:443 state=error: certificate verify failed"},
88+
{:host=>"1.0.0.0", :error=>"SSL_connect returned=1 errno=0 peeraddr=1.0.0.0:443 state=error: ssl/tls alert handshake failure"}
89+
]
8690
end
8791
end
8892

0 commit comments

Comments
 (0)