Skip to content

Commit

Permalink
Add the debugging information to the exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykubica committed Nov 7, 2024
1 parent 4288ae3 commit 0ef5212
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/tdastro/math_nodes/basic_math_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ def eval_func(**kwargs):
except Exception as problem:
# Provide more detailed logging, including the expression and parameters
# used, when we encounter a math error like divide by zero.
logger.error(
f"{type(problem)} encountered during operation: {self.expression}\n"
f"with arguments={kwargs}"
)
raise problem
new_message = f"Error during math operation '{self.expression}' with args={kwargs}"
raise type(problem)(new_message) from problem

super().__init__(eval_func, node_label=node_label, **kwargs)

Expand Down
16 changes: 16 additions & 0 deletions tests/tdastro/math_nodes/test_basic_math_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ def test_basic_math_node_fail():
_ = BasicMathNode("x + y", x=1.0)


@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_basic_math_node_error():
"""Test that we augment the error with information about the expression and parameters."""
node = BasicMathNode("y / x", x=0.0, y=1.0)
try:
node.sample_parameters()
except ZeroDivisionError as err:
assert str(err) == "Error during math operation 'y / x' with args={'x': 0.0, 'y': 1.0}"

node = BasicMathNode("sqrt(x)", x=-10.0)
try:
node.sample_parameters()
except ValueError as err:
assert str(err) == "Error during math operation 'np.sqrt(x)' with args={'x': -10.0}"


def test_basic_math_node_numpy():
"""Test that we can perform computations via a BasicMathNode."""
node_a = SingleVariableNode("a", 10.0)
Expand Down

0 comments on commit 0ef5212

Please sign in to comment.