Skip to content

Commit 9507800

Browse files
committed
fix: Simplify module loading by removing redundant error handling and introduce custom exceptions for agent and server errors
1 parent 8a7d523 commit 9507800

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

adf_core_python/core/agent/module/module_manager.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ def get_module(self, module_name: str, default_module_name: str) -> AbstractModu
5151
)
5252
class_name = default_module_name
5353

54-
try:
55-
module_class: type = self._load_module(class_name)
56-
except (ImportError, AttributeError) as e:
57-
raise RuntimeError(f"Failed to load module {class_name}") from e
54+
module_class: type = self._load_module(class_name)
5855

5956
instance = self._modules.get(module_name)
6057
if instance is not None:
@@ -80,10 +77,7 @@ def get_extend_action(
8077
action_name, default_action_name
8178
)
8279

83-
try:
84-
action_class: type = self._load_module(class_name)
85-
except (ImportError, AttributeError) as e:
86-
raise RuntimeError(f"Failed to load action {class_name}") from e
80+
action_class: type = self._load_module(class_name)
8781

8882
instance = self._actions.get(action_name)
8983
if instance is not None:
@@ -109,10 +103,7 @@ def get_channel_subscriber(
109103
channel_subscriber_name, default_channel_subscriber_name
110104
)
111105

112-
try:
113-
channel_subscriber_class: type = self._load_module(class_name)
114-
except (ImportError, AttributeError) as e:
115-
raise RuntimeError(f"Failed to load channel subscriber {class_name}") from e
106+
channel_subscriber_class: type = self._load_module(class_name)
116107

117108
instance = self._channel_subscribers.get(channel_subscriber_name)
118109
if instance is not None:
@@ -134,12 +125,7 @@ def get_message_coordinator(
134125
message_coordinator_name, default_message_coordinator_name
135126
)
136127

137-
try:
138-
message_coordinator_class: type = self._load_module(class_name)
139-
except (ImportError, AttributeError) as e:
140-
raise RuntimeError(
141-
f"Failed to load message coordinator {class_name}"
142-
) from e
128+
message_coordinator_class: type = self._load_module(class_name)
143129

144130
instance = self._message_coordinators.get(message_coordinator_name)
145131
if instance is not None:

adf_core_python/core/launcher/connect/component_launcher.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from adf_core_python.core.agent.agent import Agent
66
from adf_core_python.core.launcher.connect.connection import Connection
7+
from adf_core_python.core.launcher.connect.error.agent_error import AgentError
8+
from adf_core_python.core.launcher.connect.error.server_error import ServerError
79

810

911
class ComponentLauncher:
@@ -35,11 +37,18 @@ def connect(self, agent: Agent, _request_id: int) -> None:
3537

3638
try:
3739
connection.parse_message_from_kernel()
38-
except Exception as e:
40+
except AgentError as e:
3941
self.logger.exception(
40-
f"Agent threw an exception {e}",
42+
f"Agent error: {e}",
4143
exception=str(e),
4244
)
45+
except ServerError as e:
46+
if isinstance(e.__cause__, EOFError):
47+
self.logger.info(
48+
f"Connection closed by server (request_id={_request_id})"
49+
)
50+
else:
51+
self.logger.exception("Server error", exception=str(e))
4352

4453
def generate_request_id(self) -> int:
4554
self.request_id += 1

adf_core_python/core/launcher/connect/connection.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import rcrs_core.connection.rcrs_encoding_utils as rcrs_encoding_utils
55

6+
from adf_core_python.core.launcher.connect.error.agent_error import AgentError
7+
from adf_core_python.core.launcher.connect.error.server_error import ServerError
8+
69

710
class Connection:
811
def __init__(self, host: str, port: int) -> None:
@@ -34,12 +37,20 @@ def parse_message_from_kernel(self) -> None:
3437
3538
Raises
3639
------
37-
IOError
40+
ServerError
3841
If there is an error reading from the socket
42+
AgentError
43+
If there is an error in the agent calculation
3944
"""
4045
while True:
41-
msg = rcrs_encoding_utils.read_msg(self.socket)
42-
self.agent_message_received(msg)
46+
try:
47+
msg = rcrs_encoding_utils.read_msg(self.socket)
48+
except Exception as e:
49+
raise ServerError(f"Error reading from socket: {e}") from e
50+
try:
51+
self.agent_message_received(msg)
52+
except Exception as e:
53+
raise AgentError(f"Error agent calculation: {e}") from e
4354

4455
def message_received(self, agent_message_received: Callable) -> None:
4556
self.agent_message_received = agent_message_received
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class AgentError(Exception):
2+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ServerError(Exception):
2+
pass

0 commit comments

Comments
 (0)