Skip to content

Commit 214cc76

Browse files
committed
Verify a ConnectionError is reraised in that situation
1 parent 3605e5f commit 214cc76

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

elasticsearch/_async/transport.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ async def _do_verify_elasticsearch(self, headers, timeout):
427427

428428
info_headers = {}
429429
info_response = {}
430-
info_error = None
430+
error = None
431431

432432
for conn in chain(self.connection_pool.connections, self.seed_connections):
433433
try:
@@ -455,15 +455,20 @@ async def _do_verify_elasticsearch(self, headers, timeout):
455455
"Elasticsearch due security privileges on the server side"
456456
),
457457
ElasticsearchWarning,
458-
stacklevel=3,
458+
stacklevel=4,
459459
)
460460
self._verified_elasticsearch = True
461461
return
462462

463463
# This connection didn't work, we'll try another.
464-
except (ConnectionError, SerializationError):
465-
if info_error is None:
466-
info_error = info_error
464+
except (ConnectionError, SerializationError) as err:
465+
if error is None:
466+
error = err
467+
468+
# If we received a connection error and weren't successful
469+
# anywhere then we reraise the more appropriate error.
470+
if error and not info_response:
471+
raise error
467472

468473
# Check the information we got back from the index request.
469474
_verify_elasticsearch(info_headers, info_response)

elasticsearch/transport.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ def _do_verify_elasticsearch(self, headers, timeout):
522522

523523
info_headers = {}
524524
info_response = {}
525+
error = None
525526

526527
for conn in chain(self.connection_pool.connections, self.seed_connections):
527528
try:
@@ -549,14 +550,20 @@ def _do_verify_elasticsearch(self, headers, timeout):
549550
"Elasticsearch due security privileges on the server side"
550551
),
551552
ElasticsearchWarning,
552-
stacklevel=3,
553+
stacklevel=5,
553554
)
554555
self._verified_elasticsearch = True
555556
return
556557

557558
# This connection didn't work, we'll try another.
558-
except (ConnectionError, SerializationError):
559-
pass
559+
except (ConnectionError, SerializationError) as err:
560+
if error is None:
561+
error = err
562+
563+
# If we received a connection error and weren't successful
564+
# anywhere then we reraise the more appropriate error.
565+
if error and not info_response:
566+
raise error
560567

561568
# Check the information we got back from the index request.
562569
_verify_elasticsearch(info_headers, info_response)

test_elasticsearch/test_async/test_connection.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from mock import patch
2929
from multidict import CIMultiDict
3030

31-
from elasticsearch import AIOHttpConnection, __versionstr__
31+
from elasticsearch import AIOHttpConnection, AsyncElasticsearch, __versionstr__
3232
from elasticsearch.compat import reraise_exceptions
3333
from elasticsearch.exceptions import ConnectionError
3434

@@ -410,3 +410,9 @@ async def test_aiohttp_connection_error(self):
410410
conn = AIOHttpConnection("not.a.host.name")
411411
with pytest.raises(ConnectionError):
412412
await conn.perform_request("GET", "/")
413+
414+
async def test_elasticsearch_connection_error(self):
415+
es = AsyncElasticsearch("http://not.a.host.name")
416+
417+
with pytest.raises(ConnectionError):
418+
await es.search()

test_elasticsearch/test_connection.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from requests.auth import AuthBase
3232
from urllib3._collections import HTTPHeaderDict
3333

34-
from elasticsearch import __versionstr__
34+
from elasticsearch import Elasticsearch, __versionstr__
3535
from elasticsearch.compat import reraise_exceptions
3636
from elasticsearch.connection import (
3737
Connection,
@@ -1045,3 +1045,9 @@ def test_requests_connection_error(self):
10451045
conn = RequestsHttpConnection("not.a.host.name")
10461046
with pytest.raises(ConnectionError):
10471047
conn.perform_request("GET", "/")
1048+
1049+
def test_elasticsearch_connection_error(self):
1050+
es = Elasticsearch("http://not.a.host.name")
1051+
1052+
with pytest.raises(ConnectionError):
1053+
es.search()

0 commit comments

Comments
 (0)