Skip to content

Commit 35151c1

Browse files
committed
Error handling: Fail early when database cluster does not respond
1 parent e1dfd2e commit 35151c1

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unreleased
66
==========
77
- Modernize project definition to latest Python best practices. Thanks, @surister.
88
- Exceptions: Exceptions from the BLOB API now include their full names.
9+
- Changed connection behaviour to fail early if the database cluster does not respond
910

1011
2025/01/30 2.0.0
1112
================

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
@@ -197,14 +198,20 @@ def get_blob_container(self, container_name):
197198

198199
def _lowest_server_version(self):
199200
lowest = None
201+
last_connection_error = None
200202
for server in self.client.active_servers:
201203
try:
202204
_, _, version = self.client.server_infos(server)
203205
version = Version(version)
204-
except (ValueError, ConnectionError):
206+
except ConnectionError as ex:
207+
last_connection_error = ex
208+
continue
209+
except (ValueError, InvalidVersion):
205210
continue
206211
if not lowest or version < lowest:
207212
lowest = version
213+
if lowest is None and last_connection_error is not None:
214+
raise last_connection_error
208215
return lowest or Version("0.0.0")
209216

210217
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)