Skip to content

Commit db832e2

Browse files
committed
Error handling: Fail early when database cluster does not respond
1 parent b9dd9c0 commit db832e2

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Changed connection behaviour to fail early if the database cluster does not respond
9+
810
2026/03/09 2.1.2
911
================
1012

docs/by-example/client.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ respond, the request is automatically routed to the next server:
2929
>>> connection = client.connect([invalid_host, crate_host])
3030
>>> connection.close()
3131

32-
If no ``servers`` are given, the default one ``http://127.0.0.1:4200`` is used:
33-
34-
>>> connection = client.connect()
35-
>>> connection.client._active_servers
36-
['http://127.0.0.1:4200']
37-
>>> connection.close()
32+
If no ``servers`` are supplied to the ``connect`` method, the default address
33+
``http://127.0.0.1:4200`` is used.
3834

3935
If the option ``error_trace`` is set to ``True``, the client will print a whole
4036
traceback if a server error occurs:

src/crate/client/connection.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

2222
from verlib2 import Version
23+
from verlib2.packaging.version import InvalidVersion
2324

2425
from .blob import BlobContainer
2526
from .cursor import Cursor
@@ -201,14 +202,20 @@ def get_blob_container(self, container_name):
201202

202203
def _lowest_server_version(self):
203204
lowest = None
205+
last_connection_error = None
204206
for server in self.client.active_servers:
205207
try:
206208
_, _, version = self.client.server_infos(server)
207209
version = Version(version)
208-
except (ValueError, ConnectionError):
210+
except ConnectionError as ex:
211+
last_connection_error = ex
212+
continue
213+
except (ValueError, InvalidVersion):
209214
continue
210215
if not lowest or version < lowest:
211216
lowest = version
217+
if lowest is None and last_connection_error is not None:
218+
raise last_connection_error
212219
return lowest or Version("0.0.0")
213220

214221
def __repr__(self):

tests/client/test_connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from urllib3 import Timeout
66

7+
import crate.client.exceptions
78
from crate.client import connect
89
from crate.client.connection import Connection
910
from crate.client.exceptions import ProgrammingError
@@ -12,6 +13,13 @@
1213
from .settings import crate_host
1314

1415

16+
def test_invalid_server_address():
17+
client = Client(servers="localhost:4202")
18+
with pytest.raises(crate.client.exceptions.ConnectionError) as excinfo:
19+
connect(client=client)
20+
assert excinfo.match("Server not available")
21+
22+
1523
def test_lowest_server_version():
1624
"""
1725
Verify the lowest server version is correctly set.

0 commit comments

Comments
 (0)