Skip to content

Commit

Permalink
fix(core): VariableRenderer should expose alternativeRender
Browse files Browse the repository at this point in the history
  • Loading branch information
fhussonnois authored and Skraye committed Apr 23, 2024
1 parent 2211539 commit 00172f1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
26 changes: 22 additions & 4 deletions core/src/main/java/io/kestra/core/runners/VariableRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ public String renderOnce(String inline, Map<String, Object> variables) throws Il
Writer writer = new JsonWriter(new StringWriter());
compiledTemplate.evaluate(writer, variables);
result = writer.toString();
} catch (IOException e) {
throw new IllegalVariableEvaluationException(e);
} catch (PebbleException e) {
throw properPebbleException(e);
} catch (IOException | PebbleException e) {
String alternativeRender = this.alternativeRender(e, inline, variables);
if (alternativeRender == null) {
if (e instanceof PebbleException) {
throw properPebbleException((PebbleException) e);
}
throw new IllegalVariableEvaluationException(e);
} else {
result = alternativeRender;
}
}

// post-process raw tags
Expand All @@ -111,6 +117,18 @@ public String renderOnce(String inline, Map<String, Object> variables) throws Il
return result;
}

/**
* This method can be used in fallback for rendering an input string.
*
* @param e The exception that was throw by the default variable renderer.
* @param inline The expression to be rendered.
* @param variables The context variables.
* @return The rendered string.
*/
protected String alternativeRender(Exception e, String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
return null;
}

private static String putBackRawTags(Map<String, String> replacers, String result) {
for (var entry : replacers.entrySet()) {
result = result.replace(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.kestra.core.runners;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.micronaut.context.ApplicationContext;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

@MicronautTest
class VariableRendererTest {

@Inject
ApplicationContext applicationContext;

@Inject
VariableRenderer.VariableConfiguration variableConfiguration;

@Test
void shouldRenderUsingAlternativeRendering() throws IllegalVariableEvaluationException {
TestVariableRenderer renderer = new TestVariableRenderer(applicationContext, variableConfiguration);
String render = renderer.render("{{ dummy }}", Map.of());
Assertions.assertEquals("result", render);
}


public static class TestVariableRenderer extends VariableRenderer {

public TestVariableRenderer(ApplicationContext applicationContext,
VariableConfiguration variableConfiguration) {
super(applicationContext, variableConfiguration);
}

@Override
protected String alternativeRender(Exception e, String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
return "result";
}
}


}

0 comments on commit 00172f1

Please sign in to comment.