Search in sources :

Example 1 with IllegalVariableEvaluationException

use of io.kestra.core.exceptions.IllegalVariableEvaluationException in project kestra by kestra-io.

the class FlowableUtils method resolveEachTasks.

public static List<ResolvedTask> resolveEachTasks(RunContext runContext, TaskRun parentTaskRun, List<Task> tasks, String value) throws IllegalVariableEvaluationException {
    String renderValue = runContext.render(value);
    List<Object> values;
    try {
        values = MAPPER.readValue(renderValue, TYPE_REFERENCE);
    } catch (JsonProcessingException e) {
        throw new IllegalVariableEvaluationException(e);
    }
    List<Object> distinctValue = values.stream().distinct().collect(Collectors.toList());
    long nullCount = distinctValue.stream().filter(Objects::isNull).count();
    if (nullCount > 0) {
        throw new IllegalVariableEvaluationException("Found '" + nullCount + "' null values on Each, " + "with values=" + Arrays.toString(values.toArray()));
    }
    ArrayList<ResolvedTask> result = new ArrayList<>();
    for (Object current : distinctValue) {
        for (Task task : tasks) {
            try {
                String resolvedValue = current instanceof String ? (String) current : MAPPER.writeValueAsString(current);
                result.add(ResolvedTask.builder().task(task).value(resolvedValue).parentId(parentTaskRun.getId()).build());
            } catch (JsonProcessingException e) {
                throw new IllegalVariableEvaluationException(e);
            }
        }
    }
    return result;
}
Also used : IllegalVariableEvaluationException(io.kestra.core.exceptions.IllegalVariableEvaluationException) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) Task(io.kestra.core.models.tasks.Task) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with IllegalVariableEvaluationException

use of io.kestra.core.exceptions.IllegalVariableEvaluationException in project kestra by kestra-io.

the class VariableRenderer method recursiveRender.

public String recursiveRender(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
    if (inline == null) {
        return null;
    }
    boolean isSame = false;
    String currentTemplate = inline;
    String current = "";
    PebbleTemplate compiledTemplate;
    while (!isSame) {
        try {
            compiledTemplate = pebbleEngine.getLiteralTemplate(currentTemplate);
            Writer writer = new JsonWriter(new StringWriter());
            compiledTemplate.evaluate(writer, variables);
            current = writer.toString();
        } catch (IOException | PebbleException e) {
            if (this.variableConfiguration.disableHandlebars) {
                if (e instanceof PebbleException) {
                    throw properPebbleException((PebbleException) e);
                }
                throw new IllegalVariableEvaluationException(e);
            }
            try {
                Template template = handlebars.compileInline(currentTemplate);
                current = template.apply(variables);
            } catch (HandlebarsException | IOException hbE) {
                throw new IllegalVariableEvaluationException("Pebble evaluation failed with '" + e.getMessage() + "' " + "and Handlebars fallback failed also  with '" + hbE.getMessage() + "'", e);
            }
        }
        isSame = currentTemplate.equals(current);
        currentTemplate = current;
    }
    return current;
}
Also used : IllegalVariableEvaluationException(io.kestra.core.exceptions.IllegalVariableEvaluationException) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringWriter(java.io.StringWriter) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) IOException(java.io.IOException) JsonWriter(io.kestra.core.runners.pebble.JsonWriter) StringWriter(java.io.StringWriter) JsonWriter(io.kestra.core.runners.pebble.JsonWriter) Writer(java.io.Writer) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) Template(com.github.jknack.handlebars.Template)

Aggregations

IllegalVariableEvaluationException (io.kestra.core.exceptions.IllegalVariableEvaluationException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Template (com.github.jknack.handlebars.Template)1 PebbleException (com.mitchellbosecke.pebble.error.PebbleException)1 PebbleTemplate (com.mitchellbosecke.pebble.template.PebbleTemplate)1 ResolvedTask (io.kestra.core.models.tasks.ResolvedTask)1 Task (io.kestra.core.models.tasks.Task)1 JsonWriter (io.kestra.core.runners.pebble.JsonWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1