lib/algolia/client.rb in algoliasearch-1.1.7 vs lib/algolia/client.rb in algoliasearch-1.1.8
- old
+ new
@@ -24,10 +24,11 @@
# Perform an HTTP request for the given uri and method
# with common basic response handling. Will raise a
# AlgoliaProtocolError if the response has an error status code,
# and will return the parsed JSON body on success, if there is one.
def request(uri, method, data = nil)
+ exceptions = []
thread_local_hosts.each do |host|
begin
session = host["session"]
session.url = host["base_url"] + uri
case method
@@ -40,20 +41,20 @@
session.put(data)
when :DELETE
session.http_delete
end
if session.response_code >= 400 || session.response_code < 200
- raise AlgoliaProtocolError.new(session.response_code, "#{method} #{session.url}: #{session.body_str}")
+ raise AlgoliaProtocolError.new(session.response_code, "Cannot #{method} to #{session.url}: #{session.body_str} (#{session.response_code})")
end
return JSON.parse(session.body_str)
rescue AlgoliaProtocolError => e
- if e.code != Protocol::ERROR_TIMEOUT and e.code != Protocol::ERROR_UNAVAILABLE
- raise
- end
+ raise if e.code != Protocol::ERROR_TIMEOUT and e.code != Protocol::ERROR_UNAVAILABLE
+ exceptions << e
rescue Curl::Err::CurlError => e
+ exceptions << e
end
end
- raise AlgoliaProtocolError.new(0, "Cannot reach any hosts")
+ raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
end
def get(uri)
request(uri, :GET)
end