Skip to content

Commit

Permalink
Zyp/Moksha: Improve error reporting when rule evaluation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 25, 2024
1 parent 7799ee7 commit 774b9b2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
- DynamoDB CDC: Fix `MODIFY` operation by propagating `NewImage` fully
- Zyp/Moksha: Improve error reporting when rule evaluation fails

## 2024/09/25 v0.0.18
- MongoDB: Improved `MongoDBCrateDBConverter.decode_canonical` to also
Expand Down
2 changes: 2 additions & 0 deletions src/zyp/model/moksha.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def apply(self, data: t.Any) -> t.Any:
data = rule.evaluate(data)
except Exception:
logger.exception(f"Error evaluating rule: {rule}")
if isinstance(data, map):
data = list(data)
logger.debug(f"Error payload:\n{data}")
raise
return data
12 changes: 11 additions & 1 deletion tests/zyp/moksha/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_moksha_transformation_success_jq():
assert moksha.apply(4242) == 42.42


def test_moksha_transformation_error_jq(caplog):
def test_moksha_transformation_error_jq_scalar(caplog):
moksha = MokshaTransformation().jq(". /= 100")
with pytest.raises(ValueError) as ex:
moksha.apply("foo")
Expand All @@ -59,6 +59,16 @@ def test_moksha_transformation_error_jq(caplog):
assert "Error payload:\nfoo" in caplog.messages


def test_moksha_transformation_error_jq_map(caplog):
moksha = MokshaTransformation().jq(".foo")
with pytest.raises(ValueError) as ex:
moksha.apply(map(lambda x: x, ["foo"])) # noqa: C417
assert ex.match(re.escape('Cannot index array with string "foo"'))

assert "Error evaluating rule: MokshaRuntimeRule(type='jq'" in caplog.text
assert "Error payload:\n[]" in caplog.messages


def test_moksha_transformation_empty():
"""
Empty JSON Pointer expression means "root node".
Expand Down

0 comments on commit 774b9b2

Please sign in to comment.