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

feat: adding more descriptive errors #3319

Merged
merged 16 commits into from
Aug 8, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: moving out the generic error raising.\nPreparing for custom…
… gRPC error codes.
germa89 committed Jul 30, 2024
commit 2ebc8dc52def3f4fc6a5def680aee64aa155454d
123 changes: 68 additions & 55 deletions src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
@@ -21,13 +21,12 @@
# SOFTWARE.

"""PyMAPDL specific errors"""

from functools import wraps
import signal
import threading
from typing import Callable, Optional

from grpc._channel import _InactiveRpcError, _MultiThreadedRendezvous
import grpc

from ansys.mapdl.core import LOG as logger

@@ -309,59 +308,26 @@ def wrapper(*args, **kwargs):
# Capture gRPC exceptions
try:
out = func(*args, **kwargs)
except (_InactiveRpcError, _MultiThreadedRendezvous) as error:
# can't use isinstance here due to circular imports
try:
class_name = args[0].__class__.__name__
except:
class_name = ""

# trying to get "cmd" argument:
cmd = args[1] if len(args) >= 2 else ""
cmd = kwargs.get("cmd", cmd)

caller = func.__name__

if cmd:
msg_ = f"running:\n\t{cmd}\ncalled by:\n\t{caller}\n"
else:
msg_ = f"calling:{caller}\nwith the following arguments:\nargs: {list(*args)}\nkwargs: {list(**kwargs_)}"

if class_name == "MapdlGrpc":
mapdl = args[0]
elif hasattr(args[0], "_mapdl"):
mapdl = args[0]._mapdl

if "Received message larger than max" in error.details():
try:
lim_ = int(error.details().split("(")[1].split("vs")[0])
except IndexError:
lim_ = int(512 * 1024**2)

raise MapdlgRPCError(
f"{error.details()}. "
"You can try to increase the gRPC message length size using 'PYMAPDL_MAX_MESSAGE_LENGTH'"
" environment variable. For instance:\n\n"
f"$ export PYMAPDL_MAX_MESSAGE_LENGTH={lim_}"
)

else:
# Generic error
msg = (
f"MAPDL server connection terminated unexpectedly while {msg_}\n"
"Error:\n"
f"\t{error.details()}\n"
f"Full error:\n{error}"
)
# Test if MAPDL is alive or not.
if mapdl.is_alive:
raise MapdlRuntimeError(msg)

else:
# Must close unfinished processes
mapdl._close_process()
raise MapdlExitedError(msg)

except grpc.RpcError as error:
# Custom errors
if error.code() == grpc.StatusCode.RESOURCE_EXHAUSTED:
if "Received message larger than max" in error.details():
try:
lim_ = int(error.details().split("(")[1].split("vs")[0])
except IndexError:
lim_ = int(512 * 1024**2)

raise MapdlgRPCError(
f"RESOURCE_EXHAUSTED: {error.details()}. "
"You can try to increase the gRPC message length size using 'PYMAPDL_MAX_MESSAGE_LENGTH'"
" environment variable. For instance:\n\n"
f"$ export PYMAPDL_MAX_MESSAGE_LENGTH={lim_}"
)

# Generic error
handle_generic_grpc_error(error, func, args, kwargs)

# No exceptions
if threading.current_thread().__class__.__name__ == "_MainThread":
received_interrupt = bool(SIGINT_TRACKER)

@@ -378,6 +344,53 @@ def wrapper(*args, **kwargs):
return wrapper


def handle_generic_grpc_error(error, func, args, kwargs):
"""Handle non-custom gRPC errors"""

# can't use isinstance here due to circular imports
try:
class_name = args[0].__class__.__name__
except:
class_name = ""

# trying to get "cmd" argument:
cmd = args[1] if len(args) >= 2 else ""
cmd = kwargs.get("cmd", cmd)

caller = func.__name__

if cmd:
msg_ = f"running:\n\t{cmd}\ncalled by:\n\t{caller}\n"
else:
msg_ = f"calling:{caller}\nwith the following arguments:\nargs: {list(*args)}\nkwargs: {list(**kwargs_)}"

if class_name == "MapdlGrpc":
mapdl = args[0]
elif hasattr(args[0], "_mapdl"):
mapdl = args[0]._mapdl

msg = (
f"MAPDL server connection terminated unexpectedly while {msg_}\n"
"Error:\n"
f"\t{error.details()}\n"
f"Full error:\n{error}"
)

# MAPDL gRPC is unavailable.
if error.code() == grpc.StatusCode.UNAVAILABLE:
raise MapdlExitedError(msg)

# Generic error
# Test if MAPDL is alive or not.
if mapdl.is_alive:
raise MapdlRuntimeError(msg)

else:
# Must close unfinished processes
mapdl._close_process()
raise MapdlExitedError(msg)


def protect_from(
exception, match: Optional[str] = None, condition: Optional[bool] = None
) -> Callable: