Made some changes to authentication requests to give better message if server is down!
This commit is contained in:
+35
-12
@@ -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
|
||||
Reference in New Issue
Block a user