Skip to content

Commit 051d6ab

Browse files
committed
moved the proxy check to when the print to elog button is first clicked
1 parent ff6341e commit 051d6ab

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

trace/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
from config import logger, datetime_pv
3636
from file_io import PathAction, TraceFileHandler
3737
from widgets import ControlPanel, ElogPostModal, DataInsightTool, PlotSettingsModal
38-
from services import Theme, IconColors, ThemeManager, get_user, post_entry
38+
from services import (
39+
Theme,
40+
IconColors,
41+
ThemeManager,
42+
get_user,
43+
post_entry,
44+
test_proxy_connection,
45+
)
3946

4047
DISABLE_AUTO_SCROLL = -2 # Using -2 as invalid since QButtonGroups use -1 as invalid
4148

@@ -552,6 +559,17 @@ def elog_button_clicked(self) -> bool:
552559
bool
553560
True if the post was successful, False otherwise.
554561
"""
562+
# Test proxy connection first if proxy is configured
563+
proxy_success, proxy_error = test_proxy_connection()
564+
if not proxy_success:
565+
error_dialog = QMessageBox()
566+
error_dialog.setIcon(QMessageBox.Warning)
567+
error_dialog.setWindowTitle("Proxy Connection Failed")
568+
error_dialog.setText(proxy_error)
569+
error_dialog.setStandardButtons(QMessageBox.Ok)
570+
error_dialog.exec_()
571+
return False
572+
555573
# Test if API is reachable
556574
status_code, _ = get_user()
557575
if status_code != 200:

trace/services/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .elog_client import get_user, post_entry, get_logbooks
1+
from .elog_client import get_user, post_entry, get_logbooks, test_proxy_connection
22
from .theme_manager import ThemeManager, Theme, IconColors

trace/services/elog_client.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os
88
import json
9+
from typing import Optional
910
from pathlib import Path
1011

1112
import requests
@@ -22,24 +23,47 @@
2223
if ELOG_PROXY_URL:
2324
os.environ["HTTP_PROXY"] = ELOG_PROXY_URL
2425
os.environ["HTTPS_PROXY"] = ELOG_PROXY_URL
26+
logger.info(f"ELOG client configured to use proxy: {ELOG_PROXY_URL}")
27+
28+
29+
def test_proxy_connection() -> tuple[bool, Optional[str]]:
30+
"""
31+
Tests proxy connectivity if a proxy is configured.
32+
33+
:return: A tuple of (success, error_message). If successful, error_message is None.
34+
If failed, error_message contains a detailed description of the failure.
35+
"""
36+
if not ELOG_PROXY_URL:
37+
# No proxy configured, consider this a success
38+
return True, None
39+
40+
if not ELOG_API_URL:
41+
return False, "ELOG API URL is not configured. Cannot test proxy connection."
42+
43+
error_msg = (
44+
f"Failed to connect through proxy {ELOG_PROXY_URL}. "
45+
f"Please check your network connection and proxy configuration."
46+
)
2547

26-
# Test proxy connectivity
2748
try:
28-
test_response = requests.head(ELOG_API_URL, timeout=5)
29-
logger.info(f"ELOG client using proxy: {ELOG_PROXY_URL} - Connection test successful")
49+
requests.head(ELOG_API_URL, timeout=2)
50+
logger.info(f"Proxy connection test successful: {ELOG_PROXY_URL}")
51+
return True, None
3052
except requests.exceptions.ProxyError as e:
31-
logger.error(f"Proxy connection failed: {ELOG_PROXY_URL} is not accessible - {e}")
53+
logger.error(f"Proxy connection test failed: {e}")
54+
return False, error_msg
3255
except requests.exceptions.ConnectionError as e:
33-
logger.error(
34-
f"""Connection failed through proxy {ELOG_PROXY_URL}.
35-
Check network connectivity and proxy configuration - {e}"""
36-
)
56+
logger.error(f"Proxy connection test failed: {e}")
57+
return False, error_msg
3758
except requests.exceptions.Timeout as e:
38-
logger.error(
39-
f"Connection timeout through proxy {ELOG_PROXY_URL}.The proxy or server may be slow or unresponsive - {e}"
59+
error_msg = (
60+
f"Connection timeout through proxy {ELOG_PROXY_URL}. " f"The proxy or server may be slow or unresponsive."
4061
)
62+
logger.error(f"Proxy connection test failed: {e}")
63+
return False, error_msg
4164
except requests.exceptions.RequestException as e:
42-
logger.error(f"Proxy connection test failed for {ELOG_PROXY_URL}: {e}")
65+
logger.error(f"Proxy connection test failed: {e}")
66+
return False, error_msg
4367

4468

4569
def get_user() -> tuple[int, dict | Exception]:

0 commit comments

Comments
 (0)