From e662ce3f88d6d4776118674ee81fefef720f61e5 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 14 Mar 2023 18:49:25 +0800 Subject: [PATCH 1/4] Homogenizing error handling --- src/ansys/mapdl/core/mapdl.py | 37 +++++++++++++++++++++-------------- tests/test_errors.py | 17 ++++++++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 3c8a73a551..4c031b11ac 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -2904,18 +2904,7 @@ def run(self, command, write_to_log=True, mute=None, **kwargs) -> str: return self._response if not self.ignore_errors: - if "is not a recognized" in text: - text = text.replace("This command will be ignored.", "") - text += "\n\nIgnore these messages by setting 'ignore_errors'=True" - raise MapdlInvalidRoutineError(text) - - if "command is ignored" in text: - text += "\n\nIgnore these messages by setting 'ignore_errors'=True" - raise MapdlCommandIgnoredError(text) - - # flag errors - if "*** ERROR ***" in self._response: - self._raise_output_errors(self._response) + self._raise_errors(text) # special returns for certain geometry commands short_cmd = parse_to_short_cmd(command) @@ -3847,6 +3836,20 @@ def wrapped(self, *args, **kwargs): return wrapped(self, *args, **kwargs) + def _raise_errors(self, text): + if "is not a recognized" in text: + text = text.replace("This command will be ignored.", "") + text += "\n\nIgnore these messages by setting 'ignore_errors'=True" + raise MapdlInvalidRoutineError(text) + + if "command is ignored" in text: + text += "\n\nIgnore these messages by setting 'ignore_errors'=True" + raise MapdlCommandIgnoredError(text) + + # flag errors + if "*** ERROR ***" in text: + self._raise_output_errors(text) + def _raise_output_errors(self, response): """Raise errors in the MAPDL response. @@ -3892,9 +3895,13 @@ def _raise_output_errors(self, response): ) error_message = partial_output else: - error_message = error_message.group( - 0 - ) # Catching only the first error. + # Catching only the first error. + error_message = error_message.group(0) + + # Trimming empty lines + error_message = "\n".join( + [each for each in error_message.splitlines() if each] + ) # Checking for permitted error. for each_error in _PERMITTED_ERRORS: diff --git a/tests/test_errors.py b/tests/test_errors.py index ea4b27e086..48f63b4a4c 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,10 +1,12 @@ import pytest from ansys.mapdl.core.errors import ( + MapdlCommandIgnoredError, MapdlDidNotStart, MapdlError, MapdlException, MapdlInfo, + MapdlInvalidRoutineError, MapdlNote, MapdlRuntimeError, MapdlVersionError, @@ -100,9 +102,24 @@ def test_raise_output_errors(mapdl, response, expected_error): MapdlNote, MapdlInfo, MapdlVersionError, + MapdlInvalidRoutineError, ], ) def test_exception_classes(error_class): message = "Exception message" with pytest.raises(error_class): raise error_class(message) + + +def test_error_handler(mapdl): + text = "is not a recognized" + with pytest.raises(MapdlInvalidRoutineError, match="recognized"): + mapdl._raise_errors(text) + + text = "command is ignored" + with pytest.raises(MapdlCommandIgnoredError, match="ignored"): + mapdl._raise_errors(text) + + text = "*** ERROR ***\n This is my own errorrrr" + with pytest.raises(MapdlRuntimeError, match="errorrrr"): + mapdl._raise_errors(text) From a255db97f21221cf4901693ceff88644a35924dd Mon Sep 17 00:00:00 2001 From: German Date: Tue, 14 Mar 2023 18:58:35 +0800 Subject: [PATCH 2/4] Using same approach for input/flush stored --- src/ansys/mapdl/core/mapdl_grpc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 54b62c027e..f220e53a0a 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -1985,8 +1985,8 @@ def build_rand_tmp(): self._response = out[out.find("LINE= 0") + 13 :] self._log.info(self._response) - if "*** ERROR ***" in self._response and not self._ignore_errors: - self._raise_output_errors(self._response) + if not self._ignore_errors: + self._raise_errors(self._response) # try/except here because MAPDL might have not closed the temp file try: From e1bd688529032157765f47036a2f13d4ecdb5fbf Mon Sep 17 00:00:00 2001 From: German Date: Tue, 14 Mar 2023 19:03:00 +0800 Subject: [PATCH 3/4] Fixing tests --- tests/test_commands.py | 2 ++ tests/test_grpc.py | 6 +++--- tests/test_inline_functions/test_query.py | 10 ++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 7c2f94ef04..799261fd06 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -579,6 +579,8 @@ def test_cmd_class_dlist_vm(mapdl, cleared): # Run only the first 100 lines of VM223 with open(verif_files.vmfiles["vm223"]) as fid: cmds = fid.readlines() + + mapdl.finish() mapdl.input_strings("\n".join(cmds[:100])) mapdl.allsel("all") diff --git a/tests/test_grpc.py b/tests/test_grpc.py index 1452e0e4a9..1c0ba8bca9 100644 --- a/tests/test_grpc.py +++ b/tests/test_grpc.py @@ -6,7 +6,7 @@ from ansys.mapdl.core import examples from ansys.mapdl.core.common_grpc import DEFAULT_CHUNKSIZE -from ansys.mapdl.core.errors import MapdlRuntimeError +from ansys.mapdl.core.errors import MapdlCommandIgnoredError, MapdlRuntimeError from ansys.mapdl.core.launcher import check_valid_ansys, get_start_instance from ansys.mapdl.core.misc import random_string @@ -124,7 +124,7 @@ def test_clear_multiple(mapdl): def test_invalid_get(mapdl): - with pytest.raises(MapdlRuntimeError): + with pytest.raises((MapdlRuntimeError, MapdlCommandIgnoredError)): mapdl.get_value("ACTIVE", item1="SET", it1num="invalid") @@ -241,7 +241,7 @@ def test_read_input_file(mapdl, file_name): def test_no_get_value_non_interactive(mapdl): - with pytest.raises(RuntimeError, match="Cannot use gRPC enabled ``GET``"): + with pytest.raises((RuntimeError, MapdlCommandIgnoredError)): with mapdl.non_interactive: mapdl.get_value("ACTIVE", item1="CSYS") diff --git a/tests/test_inline_functions/test_query.py b/tests/test_inline_functions/test_query.py index 03639216f8..d7d240a8ff 100644 --- a/tests/test_inline_functions/test_query.py +++ b/tests/test_inline_functions/test_query.py @@ -1,5 +1,7 @@ import pytest +from ansys.mapdl.core.errors import MapdlCommandIgnoredError, MapdlRuntimeError + class TestParseParameter: @pytest.mark.parametrize( @@ -69,10 +71,10 @@ def test_run_query_returned_type(self, line_geometry, command): assert isinstance(v, type_) def test_interactive_mode_error(self, mapdl, line_geometry): - q, kps, l0 = line_geometry - with mapdl.non_interactive: - with pytest.raises(RuntimeError): - v = q.kx(1) + q, _, _ = line_geometry + with pytest.raises((MapdlRuntimeError, MapdlCommandIgnoredError)): + with mapdl.non_interactive: + q.kx(1) @pytest.mark.skip_grpc # only works in gRPC mode def test_nopr_mode(self, mapdl, line_geometry): From 0e6ff4f40e1657ea23f8149ab79b761e11e1789f Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:24:26 +0800 Subject: [PATCH 4/4] Update tests/test_errors.py Co-authored-by: Camille <78221213+clatapie@users.noreply.github.com> --- tests/test_errors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_errors.py b/tests/test_errors.py index 48f63b4a4c..5f514decd6 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -103,6 +103,8 @@ def test_raise_output_errors(mapdl, response, expected_error): MapdlInfo, MapdlVersionError, MapdlInvalidRoutineError, + MapdlCommandIgnoredError, + MapdlRuntimeError, ], ) def test_exception_classes(error_class):