diff --git a/CHANGES.md b/CHANGES.md index 3b1ee27..3435bf4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/zyp/model/moksha.py b/src/zyp/model/moksha.py index be8d771..6b6dd07 100644 --- a/src/zyp/model/moksha.py +++ b/src/zyp/model/moksha.py @@ -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 diff --git a/tests/zyp/moksha/test_model.py b/tests/zyp/moksha/test_model.py index 69ea97a..44370d6 100644 --- a/tests/zyp/moksha/test_model.py +++ b/tests/zyp/moksha/test_model.py @@ -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") @@ -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".