diff --git a/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/JinjaTransformationService.java b/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/JinjaTransformationService.java
index 9c66027b0ce1f..9d573fdf293dd 100644
--- a/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/JinjaTransformationService.java
+++ b/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/JinjaTransformationService.java
@@ -31,6 +31,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jinjava.Jinjava;
+import com.hubspot.jinjava.JinjavaConfig;
+import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
/**
*
@@ -45,7 +47,8 @@ public class JinjaTransformationService implements TransformationService {
private final Logger logger = LoggerFactory.getLogger(JinjaTransformationService.class);
- private Jinjava jinjava = new Jinjava();
+ private final JinjavaConfig config = JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build();
+ private final Jinjava jinjava = new Jinjava(config);
/**
* Transforms the input value
by Jinja template.
@@ -56,9 +59,11 @@ public class JinjaTransformationService implements TransformationService {
*/
@Override
public @Nullable String transform(String template, String value) throws TransformationException {
+ String transformationResult;
+ Map bindings = new HashMap<>();
+
logger.debug("about to transform '{}' by the function '{}'", value, template);
- Map bindings = new HashMap<>();
bindings.put("value", value);
try {
@@ -68,7 +73,11 @@ public class JinjaTransformationService implements TransformationService {
// ok, then value_json is null...
}
- String transformationResult = jinjava.render(template, bindings);
+ try {
+ transformationResult = jinjava.render(template, bindings);
+ } catch (FatalTemplateErrorsException e) {
+ throw new TransformationException("An error occurred while transformation. " + e.getMessage(), e);
+ }
logger.debug("transformation resulted in '{}'", transformationResult);
diff --git a/bundles/org.openhab.transform.jinja/src/test/java/org/openhab/transform/jinja/internal/JinjaTransformationServiceTest.java b/bundles/org.openhab.transform.jinja/src/test/java/org/openhab/transform/jinja/internal/JinjaTransformationServiceTest.java
index 5e94c8209d63e..c8e52f2b745ca 100644
--- a/bundles/org.openhab.transform.jinja/src/test/java/org/openhab/transform/jinja/internal/JinjaTransformationServiceTest.java
+++ b/bundles/org.openhab.transform.jinja/src/test/java/org/openhab/transform/jinja/internal/JinjaTransformationServiceTest.java
@@ -59,4 +59,60 @@ public void testQuotedStringOnly() throws TransformationException {
// Asserts
assertEquals("Hello world!", transformedResponse);
}
+
+ @Test
+ public void testJsonParsingError() throws TransformationException {
+ // when JSON binding parsing failed
+ String transformedResponse = processor.transform("Hello {{ value }}!", "{\"string\"{: \"world\"}");
+
+ // then template should be rendered
+ assertEquals("Hello {\"string\"{: \"world\"}!", transformedResponse);
+ }
+
+ @Test
+ public void testTemplateError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{{ value_json.string }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingVariableError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{ missing }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingMapKeyError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{ value_json.missing }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingVariableIsDefined() throws TransformationException {
+ // when checking missing variable
+ String transformedResponse = processor.transform("{{ missing is defined }}", "{\"string\": \"world\"}");
+
+ // then missing variable is not defined
+ assertEquals("false", transformedResponse);
+ }
+
+ @Test
+ public void testMissingMapKeyIsDefined() throws TransformationException {
+ // when checking missing map key
+ String transformedResponse = processor.transform("{{ value_json.missing is defined }}",
+ "{\"string\": \"world\"}");
+
+ // then missing map key is not defined
+ assertEquals("false", transformedResponse);
+ }
+
+ @Test
+ public void testIsDefined() throws TransformationException {
+ // when checking map key
+ String transformedResponse = processor.transform("{{ value_json.string is defined }}",
+ "{\"string\": \"world\"}");
+
+ // then map key is defined
+ assertEquals("true", transformedResponse);
+ }
}