Skip to content

Commit 774b9b2

Browse files
committed
Zyp/Moksha: Improve error reporting when rule evaluation fails
1 parent 7799ee7 commit 774b9b2

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
## 2024/09/25 v0.0.18
78
- MongoDB: Improved `MongoDBCrateDBConverter.decode_canonical` to also

src/zyp/model/moksha.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def apply(self, data: t.Any) -> t.Any:
7878
data = rule.evaluate(data)
7979
except Exception:
8080
logger.exception(f"Error evaluating rule: {rule}")
81+
if isinstance(data, map):
82+
data = list(data)
8183
logger.debug(f"Error payload:\n{data}")
8284
raise
8385
return data

tests/zyp/moksha/test_model.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_moksha_transformation_success_jq():
4949
assert moksha.apply(4242) == 42.42
5050

5151

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

6161

62+
def test_moksha_transformation_error_jq_map(caplog):
63+
moksha = MokshaTransformation().jq(".foo")
64+
with pytest.raises(ValueError) as ex:
65+
moksha.apply(map(lambda x: x, ["foo"])) # noqa: C417
66+
assert ex.match(re.escape('Cannot index array with string "foo"'))
67+
68+
assert "Error evaluating rule: MokshaRuntimeRule(type='jq'" in caplog.text
69+
assert "Error payload:\n[]" in caplog.messages
70+
71+
6272
def test_moksha_transformation_empty():
6373
"""
6474
Empty JSON Pointer expression means "root node".

0 commit comments

Comments
 (0)