Skip to content

Commit

Permalink
Optionally avoid custom node errors.
Browse files Browse the repository at this point in the history
Custom node errors, when shown directly, obscure the
real cause of the error by only showing it in the handled
exception's traceback. By default, don't raise the custom error
but the original one.

However, still allow the user to request the custom error to
be raised, because it contains useful information like the node
that errored and which inputs was using.
  • Loading branch information
pfebrer committed Oct 31, 2023
1 parent 1d61647 commit 1002a5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/sisl/nodes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# On initialization, should the node compute? If None, defaults to `lazy`.
lazy_init=None,
# The level of logs stored in the node.
log_level="INFO"
log_level="INFO",
# Whether to raise a custom error exception (e.g. NodeCalcError) By default
# it is turned off because it can obscure the real problem by not showing it
# in the last traceback frame.
raise_custom_errors=False,
)

# Temporal contexts stack. It should not be used directly by users, the aim of this
Expand Down
11 changes: 10 additions & 1 deletion src/sisl/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class Node(NDArrayOperatorsMixin):
# Whether the node has errored during the last execution
# with the current inputs.
_errored: bool
# The error that was raised during the last execution
_error: Optional[NodeError] = None

# Logs of the node's execution.
_logger: logging.Logger
Expand Down Expand Up @@ -133,6 +135,7 @@ def setup(self, *args, **kwargs):

self._outdated = True
self._errored = False
self._error = None

self._logger = logging.getLogger(
str(id(self))
Expand Down Expand Up @@ -370,12 +373,18 @@ def get(self):
self.logs += logs.stream.getvalue()
logs.close()
self._errored = True
raise NodeCalcError(self, e, evaluated_inputs)
self._error = NodeCalcError(self, e, evaluated_inputs)

Check warning on line 376 in src/sisl/nodes/node.py

View check run for this annotation

Codecov / codecov/patch

src/sisl/nodes/node.py#L376

Added line #L376 was not covered by tests

if self.context['raise_custom_errors']:
raise self._error

Check warning on line 379 in src/sisl/nodes/node.py

View check run for this annotation

Codecov / codecov/patch

src/sisl/nodes/node.py#L378-L379

Added lines #L378 - L379 were not covered by tests
else:
raise e

Check warning on line 381 in src/sisl/nodes/node.py

View check run for this annotation

Codecov / codecov/patch

src/sisl/nodes/node.py#L381

Added line #L381 was not covered by tests

self._nupdates += 1
self._prev_evaluated_inputs = evaluated_inputs
self._outdated = False
self._errored = False
self._error = None
else:
self._logger.info(f"No need to evaluate")

Expand Down

0 comments on commit 1002a5e

Please sign in to comment.