Skip to content

Commit d2bfc4c

Browse files
committed
Update assertFn to use custom error message
1 parent e94af59 commit d2bfc4c

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/jsonata/functions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,8 @@ def each(obj: Optional[Mapping], func: Any) -> Optional[list]:
18361836
#
18371837
@staticmethod
18381838
def error(message: Optional[str]) -> NoReturn:
1839-
raise jexception.JException("D3137", -1, message if message is not None else "$error() function evaluated")
1839+
raise jexception.JException("D3137", -1,
1840+
message if message is not None else "$error() function evaluated")
18401841

18411842
#
18421843
#
@@ -1851,8 +1852,8 @@ def assert_fn(condition: Optional[bool], message: Optional[str]) -> None:
18511852
raise jexception.JException("T0410", -1)
18521853

18531854
if not condition:
1854-
raise jexception.JException("D3141", -1, "$assert() statement failed")
1855-
# message: message || "$assert() statement failed"
1855+
raise jexception.JException("D3141", -1,
1856+
message if message is not None else "$assert() statement failed")
18561857

18571858
#
18581859
#

src/jsonata/jexception.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def msg(error: str, location: int, arg1: Optional[Any], arg2: Optional[Any], det
105105

106106
formatted = message
107107

108+
if formatted == "{{{message}}}":
109+
return str(arg1)
110+
108111
# Replace any {{var}} with format "{}"
109112
formatted = re.sub("\\{\\{\\w+\\}\\}", "{}", formatted)
110113

tests/array_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import jsonata
2+
import pytest
23

34

45
class TestArray:
@@ -31,3 +32,9 @@ def test_wildcard_filter(self):
3132

3233
expression2 = jsonata.Jsonata("**[value.Product = 'Product1']")
3334
assert expression2.evaluate(data) == value1
35+
36+
def test_assert_custom_message(self):
37+
expr = jsonata.Jsonata("$assert(false, 'custom error')")
38+
with pytest.raises(jsonata.JException) as exc_info:
39+
expr.evaluate(None)
40+
assert "custom error" in str(exc_info.value)

0 commit comments

Comments
 (0)