Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Verify a ConnectionError is reraised in that situation
  • Loading branch information
sethmlarson committed Jun 29, 2021
commit 214cc766db6b8d23f626528c987fe6d6b0d75616
15 changes: 10 additions & 5 deletions elasticsearch/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ async def _do_verify_elasticsearch(self, headers, timeout):

info_headers = {}
info_response = {}
info_error = None
error = None

for conn in chain(self.connection_pool.connections, self.seed_connections):
try:
Expand Down Expand Up @@ -455,15 +455,20 @@ async def _do_verify_elasticsearch(self, headers, timeout):
"Elasticsearch due security privileges on the server side"
),
ElasticsearchWarning,
stacklevel=3,
stacklevel=4,
)
self._verified_elasticsearch = True
return

# This connection didn't work, we'll try another.
except (ConnectionError, SerializationError):
if info_error is None:
info_error = info_error
except (ConnectionError, SerializationError) as err:
if error is None:
error = err

# If we received a connection error and weren't successful
# anywhere then we reraise the more appropriate error.
if error and not info_response:
raise error

# Check the information we got back from the index request.
_verify_elasticsearch(info_headers, info_response)
Expand Down
13 changes: 10 additions & 3 deletions elasticsearch/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def _do_verify_elasticsearch(self, headers, timeout):

info_headers = {}
info_response = {}
error = None

for conn in chain(self.connection_pool.connections, self.seed_connections):
try:
Expand Down Expand Up @@ -549,14 +550,20 @@ def _do_verify_elasticsearch(self, headers, timeout):
"Elasticsearch due security privileges on the server side"
),
ElasticsearchWarning,
stacklevel=3,
stacklevel=5,
)
self._verified_elasticsearch = True
return

# This connection didn't work, we'll try another.
except (ConnectionError, SerializationError):
pass
except (ConnectionError, SerializationError) as err:
if error is None:
error = err

# If we received a connection error and weren't successful
# anywhere then we reraise the more appropriate error.
if error and not info_response:
raise error

# Check the information we got back from the index request.
_verify_elasticsearch(info_headers, info_response)
Expand Down
8 changes: 7 additions & 1 deletion test_elasticsearch/test_async/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from mock import patch
from multidict import CIMultiDict

from elasticsearch import AIOHttpConnection, __versionstr__
from elasticsearch import AIOHttpConnection, AsyncElasticsearch, __versionstr__
from elasticsearch.compat import reraise_exceptions
from elasticsearch.exceptions import ConnectionError

Expand Down Expand Up @@ -410,3 +410,9 @@ async def test_aiohttp_connection_error(self):
conn = AIOHttpConnection("not.a.host.name")
with pytest.raises(ConnectionError):
await conn.perform_request("GET", "/")

async def test_elasticsearch_connection_error(self):
es = AsyncElasticsearch("http://not.a.host.name")

with pytest.raises(ConnectionError):
await es.search()
8 changes: 7 additions & 1 deletion test_elasticsearch/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from requests.auth import AuthBase
from urllib3._collections import HTTPHeaderDict

from elasticsearch import __versionstr__
from elasticsearch import Elasticsearch, __versionstr__
from elasticsearch.compat import reraise_exceptions
from elasticsearch.connection import (
Connection,
Expand Down Expand Up @@ -1045,3 +1045,9 @@ def test_requests_connection_error(self):
conn = RequestsHttpConnection("not.a.host.name")
with pytest.raises(ConnectionError):
conn.perform_request("GET", "/")

def test_elasticsearch_connection_error(self):
es = Elasticsearch("http://not.a.host.name")

with pytest.raises(ConnectionError):
es.search()