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

Zyp/Moksha: Improve error reporting when rule evaluation fails #61

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
data = rule.evaluate(data)
except Exception:
logger.exception(f"Error evaluating rule: {rule}")
if isinstance(data, map):
data = list(data)

Check warning on line 82 in src/zyp/model/moksha.py

View check run for this annotation

Codecov / codecov/patch

src/zyp/model/moksha.py#L81-L82

Added lines #L81 - L82 were not covered by tests
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