Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homogenising error handling #1923

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import pytest

from ansys.mapdl.core.errors import (
MapdlCommandIgnoredError,
MapdlDidNotStart,
MapdlError,
MapdlException,
MapdlInfo,
MapdlInvalidRoutineError,
MapdlNote,
MapdlRuntimeError,
MapdlVersionError,
Expand Down Expand Up @@ -100,9 +102,24 @@ def test_raise_output_errors(mapdl, response, expected_error):
MapdlNote,
MapdlInfo,
MapdlVersionError,
MapdlInvalidRoutineError,
germa89 marked this conversation as resolved.
Show resolved Hide resolved
],
)
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)
6 changes: 3 additions & 3 deletions tests/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")


Expand Down Expand Up @@ -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")

Expand Down
10 changes: 6 additions & 4 deletions tests/test_inline_functions/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from ansys.mapdl.core.errors import MapdlCommandIgnoredError, MapdlRuntimeError


class TestParseParameter:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -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):
Expand Down