Skip to content

Commit 23ed2d7

Browse files
committed
Attempt loading goofi patches with invalid params instead of crashing
1 parent 904d9e1 commit 23ed2d7

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/goofi/node.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from goofi.data import Data, DataType
1616
from goofi.message import Message, MessageType
1717
from goofi.node_helpers import InputSlot, NodeRef, OutputSlot
18-
from goofi.params import NodeParams
18+
from goofi.params import InvalidParamError, NodeParams
1919

2020

2121
class MultiprocessingForbiddenError(Exception):
@@ -496,7 +496,15 @@ def create(cls, initial_params: Optional[Dict[str, Dict[str, Any]]] = None, retr
496496
in_slots, out_slots, params = cls._configure(cls)
497497
# integrate initial parameters if they are provided
498498
if initial_params is not None:
499-
params.update(initial_params)
499+
try:
500+
params.update(initial_params)
501+
except InvalidParamError:
502+
print(
503+
"\n====================== ERROR ======================\n"
504+
f"Setting parameters failed for {cls.__name__}. This is likely due to updates"
505+
" in the node's code. Make sure to reconfigure the node."
506+
"\n===================================================\n"
507+
)
500508

501509
tries = 0
502510
while True:
@@ -540,9 +548,19 @@ def create_local(cls, initial_params: Optional[Dict[str, Dict[str, Any]]] = None
540548
"""
541549
# generate arguments for the node
542550
in_slots, out_slots, params = cls._configure(cls)
551+
543552
# integrate initial parameters if they are provided
544553
if initial_params is not None:
545-
params.update(initial_params)
554+
try:
555+
params.update(initial_params)
556+
except InvalidParamError:
557+
print(
558+
"\n====================== ERROR ======================\n"
559+
f"Setting parameters failed for {cls.__name__}. This is likely due to updates"
560+
" in the node's code. Make sure to reconfigure the node."
561+
"\n===================================================\n"
562+
)
563+
546564
conn1, conn2 = Connection.create()
547565
# instantiate the node in the current process
548566
node = cls(conn2, in_slots, out_slots, params, NodeEnv.LOCAL)

src/goofi/params.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ def add_doc_attribute(cls):
143143
}
144144

145145

146+
class InvalidParamError(Exception):
147+
pass
148+
149+
146150
class NodeParams:
147151
"""
148152
A class for storing node parameters, which store the configuration state of the node, and are exposed
@@ -238,13 +242,20 @@ def update(self, params: Dict[str, Dict[str, Any]]):
238242
`params` : Dict[str, Dict[str, Any]]
239243
A dictionary of parameter groups, where each group is a dictionary of parameter names and values.
240244
"""
245+
encountered_invalid_params = False
246+
241247
# TODO: avoid code duplication with __init__
242248
for group, params in params.items():
249+
if group not in self._data:
250+
encountered_invalid_params = True
251+
print(f"Invalid parameter group '{group}'.")
252+
continue
253+
243254
for name, param in params.items():
244-
if group not in self._data:
245-
raise ValueError(f"Parameter group '{group}' does not exist.")
246255
if name not in self._data[group]._fields:
247-
raise ValueError(f"Parameter '{name}' does not exist in group '{group}'.")
256+
encountered_invalid_params = True
257+
print(f"Invalid parameter '{name}' in group '{group}'.")
258+
continue
248259

249260
if not isinstance(param, Param):
250261
if isinstance(param, dict):
@@ -265,6 +276,9 @@ def update(self, params: Dict[str, Dict[str, Any]]):
265276
# update the entire parameter object
266277
self._data[group] = self._data[group]._replace(**{name: param})
267278

279+
if encountered_invalid_params:
280+
raise InvalidParamError("Invalid parameters encountered.")
281+
268282
def serialize(self) -> Dict[str, Dict[str, Any]]:
269283
"""
270284
Serialize the parameter values to a dictionary.

0 commit comments

Comments
 (0)