Skip to content

Commit 18f0a29

Browse files
feat: Improved error handling (#3646)
* fix: Improved error handling * fix: Improved error handling * fix: Improved error handling * fix: Improved error handling * fix: restructuring * Update src/ansys/fluent/core/launcher/error_handler.py * fix: test fix * Update src/ansys/fluent/core/launcher/server_info.py Co-authored-by: Sean Pearson <[email protected]> * fix: Improvements * fix: Improvements 1 --------- Co-authored-by: Sean Pearson <[email protected]>
1 parent 1e95fde commit 18f0a29

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

src/ansys/fluent/core/launcher/error_handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def __init__(self):
2929
super().__init__("Provide either 'ip' and 'port' or 'server_info_file_name'.")
3030

3131

32+
class InvalidIpPort(ValueError):
33+
"""Raised when IP address and port are invalid."""
34+
35+
def __init__(self):
36+
"""Initialize InvalidIpPort."""
37+
super().__init__("Provide a valid 'ip' and 'port'.")
38+
39+
3240
class UnexpectedKeywordArgument(TypeError):
3341
"""Raised when a valid keyword argument is not specified."""
3442

src/ansys/fluent/core/launcher/server_info.py

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

77
from ansys.fluent.core.fluent_connection import PortNotProvided
88
from ansys.fluent.core.launcher import launcher_utils
9-
from ansys.fluent.core.launcher.error_handler import IpPortNotProvided
9+
from ansys.fluent.core.launcher.error_handler import InvalidIpPort, IpPortNotProvided
1010
from ansys.fluent.core.session import _parse_server_info_file
1111

1212

@@ -45,6 +45,27 @@ def _get_server_info_file_names(use_tmpdir=True) -> tuple[str, str]:
4545
return file_name, file_name
4646

4747

48+
def _check_ip_port(ip: str, port: int):
49+
"""Check if a port is open on a given IP address."""
50+
51+
if not (ip and port):
52+
raise IpPortNotProvided()
53+
54+
if not port:
55+
raise PortNotProvided()
56+
57+
import socket
58+
59+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
60+
sock.settimeout(2)
61+
try:
62+
result = sock.connect_ex((ip, port))
63+
if result != 0:
64+
raise InvalidIpPort()
65+
finally:
66+
sock.close()
67+
68+
4869
def _get_server_info(
4970
server_info_file_name: str,
5071
ip: str | None = None,
@@ -65,7 +86,6 @@ def _get_server_info(
6586
ip = ip or os.getenv("PYFLUENT_FLUENT_IP", "127.0.0.1")
6687
port = port or os.getenv("PYFLUENT_FLUENT_PORT")
6788

68-
if not port:
69-
raise PortNotProvided()
89+
_check_ip_port(ip=ip, port=port)
7090

7191
return ip, port, password

tests/test_launcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ansys.fluent.core.launcher import launcher_utils
1414
from ansys.fluent.core.launcher.error_handler import (
1515
GPUSolverSupportError,
16+
InvalidIpPort,
1617
LaunchFluentError,
1718
_raise_non_gui_exception_in_windows,
1819
)
@@ -534,3 +535,8 @@ def test_container_ports():
534535
with pyfluent.launch_fluent(container_dict=container_dict) as session:
535536
session._container.reload()
536537
assert len(session._container.ports) == 2
538+
539+
540+
def test_correct_ip_port():
541+
with pytest.raises(InvalidIpPort):
542+
pyfluent.connect_to_fluent(ip="1.2.3.4", port=5555)

tests/test_session.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,6 @@ def mock_parse_server_info_file(file_name):
435435
assert ex.value.__context__.__context__.code() == grpc.StatusCode.UNAVAILABLE
436436

437437

438-
def test_recover_grpc_error_from_connection_error():
439-
with pytest.raises(RuntimeError) as ex:
440-
pyfluent.connect_to_fluent(ip="127.0.0.1", port=50000, password="abcdefg")
441-
assert ex.value.__context__.code() == grpc.StatusCode.UNAVAILABLE
442-
443-
444438
def test_solver_methods(new_solver_session):
445439
solver = new_solver_session
446440

0 commit comments

Comments
 (0)