Search in sources :

Example 1 with HandlebarsException

use of com.github.jknack.handlebars.HandlebarsException in project elide by yahoo.

the class ColumnContext method get.

public Object get(Object key) {
    String keyStr = key.toString();
    // Physical References starts with $
    if (keyStr.lastIndexOf('$') == 0) {
        return resolvePhysicalReference(this, keyStr);
    }
    if (keyStr.equals(TBL_PREFIX)) {
        return TableSubContext.tableSubContextBuilder().tableArguments(this.tableArguments).build();
    }
    if (keyStr.equals(COL_PREFIX)) {
        return ColumnSubContext.columnSubContextBuilder().queryable(this.getQueryable()).alias(this.getAlias()).metaDataStore(this.getMetaDataStore()).column(this.getColumn()).tableArguments(this.getTableArguments()).build();
    }
    if (this.queryable.hasJoin(keyStr)) {
        return getJoinContext(keyStr);
    }
    // Check if key exists in Map.
    Object value = getOrDefault(key, null);
    if (value != null) {
        return value;
    }
    // In case of colA references colB and user query has both colA and colB,
    // we should use default arguments for colB while resolving colA instead of user provided argument for colB.
    // so taking colB's details from current queryable's source.
    ColumnProjection column = this.getQueryable().getSource().getColumnProjection(keyStr);
    if (column != null) {
        ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), this.getColumn().getArguments()));
        return getNewContext(this, newColumn).resolve(newColumn.getExpression());
    }
    throw new HandlebarsException(new Throwable("Couldn't find: " + keyStr));
}
Also used : ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString) HandlebarsException(com.github.jknack.handlebars.HandlebarsException)

Example 2 with HandlebarsException

use of com.github.jknack.handlebars.HandlebarsException in project kestra by kestra-io.

the class HandleBarDeserializer method deserialize.

@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    String value = p.getText();
    if (value.contains("[[") && value.contains("]]")) {
        String contextPath = String.valueOf(ctxt.findInjectableValue(YamlFlowParser.CONTEXT_FLOW_DIRECTORY, null, null));
        TemplateLoader loader = new FileTemplateLoader(contextPath, "");
        Handlebars handlebars = HANDLEBARS.with(loader);
        Template template = handlebars.compileInline(value);
        try {
            return template.apply(ImmutableMap.of());
        } catch (IOException | HandlebarsException e) {
            throw new ConstraintViolationException(Collections.singleton(ManualConstraintViolation.of(e.getMessage(), this, HandleBarDeserializer.class, "template", value)));
        }
    }
    return super.deserialize(p, ctxt);
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) TemplateLoader(com.github.jknack.handlebars.io.TemplateLoader) ConstraintViolationException(javax.validation.ConstraintViolationException) IOException(java.io.IOException) HandlebarsException(com.github.jknack.handlebars.HandlebarsException) Template(com.github.jknack.handlebars.Template)

Example 3 with HandlebarsException

use of com.github.jknack.handlebars.HandlebarsException in project molgenis by molgenis.

the class HandlebarsMathHelperTest method testExceptions.

@ParameterizedTest
@MethodSource("exceptionInputProvider")
void testExceptions(String templateString, String exception) throws IOException {
    Handlebars handlebars = new Handlebars();
    handlebars.registerHelper("math", new HandlebarsMathHelper());
    Template template = handlebars.compileInline(templateString);
    HandlebarsException thrown = assertThrows(HandlebarsException.class, () -> template.apply(null));
    assertThat(thrown.getCause().toString()).contains(exception);
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) HandlebarsException(com.github.jknack.handlebars.HandlebarsException) Template(com.github.jknack.handlebars.Template) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with HandlebarsException

use of com.github.jknack.handlebars.HandlebarsException in project vertx-web by vert-x3.

the class HandlebarsTemplateTest method testTemplateJsonArrayResolverError.

@Test
public void testTemplateJsonArrayResolverError() throws Exception {
    TemplateEngine engine = HandlebarsTemplateEngine.create();
    JsonArray jsonArray = new JsonArray();
    jsonArray.add("badger").add("fox").add(new JsonObject().put("name", "joe"));
    AtomicReference<RoutingContext> contextRef = new AtomicReference<>();
    router.route().handler(context -> {
        contextRef.set(context);
        context.put("foo", jsonArray);
        context.next();
    });
    router.route().handler(TemplateHandler.create(engine, "src/test/filesystemtemplates", "text/plain"));
    testRequest(HttpMethod.GET, "/" + "test-handlebars-template6.hbs", 500, "Internal Server Error");
    if (contextRef.get().failure() instanceof HandlebarsException) {
        HandlebarsException exception = ((HandlebarsException) contextRef.get().failure());
        assertTrue(exception.getMessage().contains("test-handlebars-template6.hbs:1:19"));
    } else {
        fail("We would expect an handlebars exception with detailed location information.");
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) HandlebarsException(com.github.jknack.handlebars.HandlebarsException) Test(org.junit.Test)

Example 5 with HandlebarsException

use of com.github.jknack.handlebars.HandlebarsException in project elide by yahoo.

the class ColumnContext method resolveSQLHandlebar.

private Object resolveSQLHandlebar(final Object context, final Options options) throws UnsupportedEncodingException {
    String from = options.hash("from");
    String columnName = options.hash("column");
    int argsIndex = columnName.indexOf('[');
    String invokedColumnName = columnName;
    ColumnContext currentCtx = (ColumnContext) context;
    // 'from' is optional, so if not provided use the same table context.
    ColumnContext invokedCtx = isBlank(from) ? currentCtx : (ColumnContext) currentCtx.get(from);
    Map<String, Argument> pinnedArgs = new HashMap<>();
    if (argsIndex >= 0) {
        pinnedArgs = getArgumentMapFromString(columnName.substring(argsIndex));
        invokedColumnName = columnName.substring(0, argsIndex);
    }
    // Physical References starts with $
    if (invokedColumnName.lastIndexOf('$') == 0) {
        return resolvePhysicalReference(invokedCtx, invokedColumnName);
    }
    ColumnProjection column = invokedCtx.getQueryable().getSource().getColumnProjection(invokedColumnName);
    if (column != null) {
        ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), invokedCtx.getColumn().getArguments(), pinnedArgs));
        return getNewContext(invokedCtx, newColumn).resolve(newColumn.getExpression());
    }
    throw new HandlebarsException(new Throwable("Couldn't find: " + invokedColumnName));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) HashMap(java.util.HashMap) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString) HandlebarsException(com.github.jknack.handlebars.HandlebarsException)

Aggregations

HandlebarsException (com.github.jknack.handlebars.HandlebarsException)5 Handlebars (com.github.jknack.handlebars.Handlebars)2 Template (com.github.jknack.handlebars.Template)2 Argument.getArgumentMapFromString (com.yahoo.elide.core.request.Argument.getArgumentMapFromString)2 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)2 ToString (lombok.ToString)2 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)1 Argument (com.yahoo.elide.core.request.Argument)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 HandlebarsTemplateEngine (io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Test (org.junit.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1