Skip to content

Commit 2aaac69

Browse files
committed
[ModelExecutionException] catch exception if ModelExecutionCmd.run() is used
1 parent 2c34957 commit 2aaac69

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ def get_exe(self) -> pathlib.Path:
209209
return path_exe
210210

211211
def get_cmd(self) -> list:
212-
"""Get a list with the path to the executable and all command line args.
212+
"""
213+
Get a list with the path to the executable and all command line args.
213214
214215
This can later be used as an argument for subprocess.run().
215216
"""
@@ -218,6 +219,10 @@ def get_cmd(self) -> list:
218219

219220
return cmdl
220221

221-
def run(self):
222+
def run(self) -> int:
222223
cmd_definition = self.definition()
223-
return cmd_definition.run()
224+
try:
225+
returncode = cmd_definition.run()
226+
except ModelExecutionException as exc:
227+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
228+
return returncode

OMPython/modelica_doe_abc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from OMPython.model_execution import (
1616
ModelExecutionData,
17+
ModelExecutionException,
1718
)
1819
from OMPython.om_session_abc import (
1920
OMPathABC,
@@ -310,8 +311,8 @@ def worker(worker_id, task_queue):
310311
returncode = cmd_definition.run()
311312
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
312313
f"finished with return code: {returncode}")
313-
except ModelicaSystemError as ex:
314-
logger.warning(f"Simulation error for {resultpath.name}: {ex}")
314+
except ModelExecutionException as exc:
315+
logger.warning(f"Simulation error for {resultpath.name}: {exc}")
315316

316317
# Mark the task as done
317318
task_queue.task_done()

OMPython/modelica_system_abc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from OMPython.model_execution import (
2020
ModelExecutionCmd,
21+
ModelExecutionException,
2122
)
2223
from OMPython.om_session_abc import (
2324
OMPathABC,
@@ -199,7 +200,10 @@ def check_model_executable(self):
199200
# ... by running it - output help for command help
200201
om_cmd.arg_set(key="help", val="help")
201202
cmd_definition = om_cmd.definition()
202-
returncode = cmd_definition.run()
203+
try:
204+
returncode = cmd_definition.run()
205+
except ModelExecutionException as exc:
206+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
203207
if returncode != 0:
204208
raise ModelicaSystemError("Model executable not working!")
205209

@@ -730,7 +734,10 @@ def simulate(
730734
self._result_file.unlink()
731735
# ... run simulation ...
732736
cmd_definition = om_cmd.definition()
733-
returncode = cmd_definition.run()
737+
try:
738+
returncode = cmd_definition.run()
739+
except ModelExecutionException as exc:
740+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
734741
# and check returncode *AND* resultfile
735742
if returncode != 0 and self._result_file.is_file():
736743
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1173,7 +1180,10 @@ def linearize(
11731180
linear_file.unlink(missing_ok=True)
11741181

11751182
cmd_definition = om_cmd.definition()
1176-
returncode = cmd_definition.run()
1183+
try:
1184+
returncode = cmd_definition.run()
1185+
except ModelExecutionException as exc:
1186+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
11771187
if returncode != 0:
11781188
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
11791189
if not linear_file.is_file():

0 commit comments

Comments
 (0)