From 6bda961231d0cdf4bf52d38846e8b2f8fb3dc652 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Mon, 9 Mar 2026 17:14:18 +0100 Subject: [PATCH] Made some changes to authentication requests to give better message if server is down! --- src/aare/gui/auth.py | 47 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/aare/gui/auth.py b/src/aare/gui/auth.py index 6c4f576f..00655c81 100644 --- a/src/aare/gui/auth.py +++ b/src/aare/gui/auth.py @@ -17,26 +17,49 @@ def auth(base_url: str | None) -> str: pgroups=["p16371", "p22233"]) return jwt.encode(token_data.model_dump(), "ABC123") + url = f"{base_url}/token" try: response = requests.post( - f"{base_url}/token", + url, data={ "username": curr_user, "password": "" }, headers={ "Content-Type": "application/x-www-form-urlencoded" - } + }, + timeout=(2.0, 5.0), # (connect timeout, read timeout) ) - if response.status_code == 200: - response_json = response.json() - if "access_token" in response_json: - return response_json["access_token"] - else: - logger.error(f"Authentication request failed: {response.content}") - raise Exception("Authentication request failed") - except requests.RequestException as e: - logger.error(f"Authentication request failed: {e}") + logger.error(f"Authentication request failed (network): {e}") + raise RuntimeError( + "Cannot reach AareDAQ server (network error). " + "Please check the server is running and your connection." + ) from e - return "" + if response.status_code != 200: + # Avoid dumping full HTML/tracebacks into the GUI; keep it short and actionable + logger.error(f"Authentication request failed: HTTP {response.status_code}. Body: {response.text[:500]}") + raise RuntimeError( + f"Authentication failed (HTTP {response.status_code}). " + "The server may be starting up or unavailable." + ) + + try: + response_json = response.json() + except ValueError as e: + logger.error(f"Authentication response was not JSON. Body: {response.text[:500]}") + raise RuntimeError( + "Authentication failed (invalid server response). " + "The server may be starting up or misconfigured." + ) from e + + token = response_json.get("access_token") + if not token or not isinstance(token, str): + logger.error(f"Authentication response missing access_token. Keys: {list(response_json.keys())}") + raise RuntimeError( + "Authentication failed (missing token in server response). " + "The server may be starting up." + ) + + return token \ No newline at end of file