From c637477cfdce7063982d540e86173b33c79a0d9e Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Fri, 19 Jan 2024 10:48:58 +0100 Subject: [PATCH] Return error message in can_connect --- src/scitacean/testing/backend/__init__.py | 3 --- src/scitacean/testing/backend/_backend.py | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/scitacean/testing/backend/__init__.py b/src/scitacean/testing/backend/__init__.py index 619f6623..612a162e 100644 --- a/src/scitacean/testing/backend/__init__.py +++ b/src/scitacean/testing/backend/__init__.py @@ -63,7 +63,6 @@ def test_manual_client(require_scicat_backend, scicat_access): add_pytest_option backend_enabled - can_connect configure skip_if_not_backend start_backend @@ -73,7 +72,6 @@ def test_manual_client(require_scicat_backend, scicat_access): from . import config, seed from ._backend import ( - can_connect, configure, start_backend, stop_backend, @@ -84,7 +82,6 @@ def test_manual_client(require_scicat_backend, scicat_access): __all__ = [ "add_pytest_option", "backend_enabled", - "can_connect", "config", "configure", "seed", diff --git a/src/scitacean/testing/backend/_backend.py b/src/scitacean/testing/backend/_backend.py index eb0a00ea..866666fa 100644 --- a/src/scitacean/testing/backend/_backend.py +++ b/src/scitacean/testing/backend/_backend.py @@ -75,13 +75,14 @@ def stop_backend(docker_compose_file: _PathLike) -> None: docker_compose_down(docker_compose_file) -def can_connect() -> bool: +def _can_connect() -> tuple[bool, str]: """Test the connection to the testing SciCat backend. Returns ------- : - True if the backend is reachable, False otherwise. + The first element indicates whether the connection was successful. + The second element is an error message. """ scicat_access = config.local_access("user1") try: @@ -90,9 +91,11 @@ def can_connect() -> bool: json=scicat_access.user.credentials, timeout=0.5, ) - except requests.ConnectionError: - return False - return response.ok + except requests.ConnectionError as err: + return False, str(err) + if response.ok: + return True, "" + return False, str(f"{response}: {response.text}") def wait_until_backend_is_live(max_time: float, n_tries: int) -> None: @@ -116,8 +119,9 @@ def wait_until_backend_is_live(max_time: float, n_tries: int) -> None: If no connection can be made within the time limit. """ for _ in range(n_tries): - if can_connect(): + if _can_connect()[0]: return time.sleep(max_time / n_tries) - if not can_connect(): - raise RuntimeError("Cannot connect to backend") + ok, err = _can_connect() + if not ok: + raise RuntimeError(f"Cannot connect to backend: {err}")