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 30, 2024
1 parent 9688e9a commit 4755b53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- DynamoDB: Change CrateDB data model to use (`pk`, `data`, `aux`) columns
Attention: This is a breaking change.
- MongoDB: Handle too large `$date.$numberLong` values gracefully
- Zyp/Moksha: Improve error reporting when rule evaluation fails

## 2024/09/26 v0.0.19
- DynamoDB CDC: Fix `MODIFY` operation by propagating `NewImage` fully
Expand Down
16 changes: 10 additions & 6 deletions src/zyp/model/moksha.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ class MokshaRule:
disabled: t.Optional[bool] = False

def compile(self):
return MokshaRuntimeRule(self.type, compile_expression(self.type, self.expression), disabled=self.disabled)
return MokshaRuntimeRule(
self, self.type, compile_expression(self.type, self.expression), disabled=self.disabled
)


@define
class MokshaRuntimeRule:
source: MokshaRule
type: str
transformer: MokshaTransformer
disabled: t.Optional[bool] = False
Expand Down Expand Up @@ -76,10 +79,11 @@ def apply(self, data: t.Any) -> t.Any:
for rule in self._runtime_rules:
try:
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}")
except Exception as ex:
logger.error(f"Error evaluating rule: {ex}. Expression: {rule.source.expression}")
if logger.level is logging.DEBUG:
if isinstance(data, map):
data = list(data)
logger.debug(f"Error payload:\n{data}")
raise
return data
10 changes: 8 additions & 2 deletions tests/zyp/moksha/test_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re

import pytest
Expand Down Expand Up @@ -50,22 +51,27 @@ def test_moksha_transformation_success_jq():


def test_moksha_transformation_error_jq_scalar(caplog):
logging.getLogger("zyp.model.moksha").setLevel(logging.DEBUG)
moksha = MokshaTransformation().jq(". /= 100")
with pytest.raises(ValueError) as ex:
moksha.apply("foo")
assert ex.match(re.escape('string ("foo") and number (100) cannot be divided'))

assert "Error evaluating rule: MokshaRuntimeRule(type='jq'" in caplog.text
assert (
'Error evaluating rule: string ("foo") and number (100) cannot be divided. Expression: . /= 100'
in caplog.messages
)
assert "Error payload:\nfoo" in caplog.messages


def test_moksha_transformation_error_jq_map(caplog):
logging.getLogger("zyp.model.moksha").setLevel(logging.DEBUG)
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 evaluating rule: Cannot index array with string "foo". Expression: .foo' in caplog.messages
assert "Error payload:\n[]" in caplog.messages


Expand Down

0 comments on commit 4755b53

Please sign in to comment.