Search in sources :

Example 1 with SoyTofuException

use of com.google.template.soy.tofu.SoyTofuException in project closure-templates by google.

the class BaseTofu method renderMainHelper.

/**
 * Renders a template and appends the result to a StringBuilder.
 *
 * @param templateRegistry A registry of all templates.
 * @param outputBuf The Appendable to append the rendered text to.
 * @param templateName The full name of the template to render.
 * @param data The data to call the template with. Can be null if the template has no parameters.
 * @param ijData The injected data to call the template with. Can be null if not used.
 * @param activeDelPackageNames The set of active delegate package names.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @param cssRenamingMap Map for renaming selectors in 'css' tags, or null if not used.
 * @return The template that was rendered.
 */
private TemplateNode renderMainHelper(TemplateRegistry templateRegistry, Appendable outputBuf, String templateName, @Nullable SoyRecord data, @Nullable SoyRecord ijData, Predicate<String> activeDelPackageNames, @Nullable SoyMsgBundle msgBundle, @Nullable SoyIdRenamingMap idRenamingMap, @Nullable SoyCssRenamingMap cssRenamingMap, boolean debugSoyTemplateInfo) {
    TemplateNode template = templateRegistry.getBasicTemplate(templateName);
    if (template == null) {
        throw new SoyTofuException("Attempting to render undefined template '" + templateName + "'.");
    } else if (template.getVisibility() == Visibility.PRIVATE) {
        throw new SoyTofuException("Attempting to render private template '" + templateName + "'.");
    }
    if (data == null) {
        data = SoyValueConverter.EMPTY_DICT;
    }
    if (ijData == null) {
        ijData = SoyValueConverter.EMPTY_DICT;
    }
    try {
        RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), outputBuf, templateRegistry, data, ijData, activeDelPackageNames, msgBundle, idRenamingMap, cssRenamingMap, debugSoyTemplateInfo);
        rv.exec(template);
    } catch (RenderException re) {
        throw new SoyTofuException(re);
    }
    return template;
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyTofuException(com.google.template.soy.tofu.SoyTofuException) RenderException(com.google.template.soy.sharedpasses.render.RenderException) EvalVisitorFactoryImpl(com.google.template.soy.sharedpasses.render.EvalVisitorFactoryImpl) RenderVisitor(com.google.template.soy.sharedpasses.render.RenderVisitor)

Example 2 with SoyTofuException

use of com.google.template.soy.tofu.SoyTofuException in project closure-templates by google.

the class TofuExceptionsTest method testExceptions_transclusion_failedFuture.

@Test
public void testExceptions_transclusion_failedFuture() {
    Exception futureFailureCause = new Exception("boom");
    SoyDict data = SoyValueConverterUtility.newDict("foo", immediateFailedFuture(futureFailureCause));
    try {
        tofu.newRenderer("ns.transclusionCaller").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        SoyFutureException sfe = (SoyFutureException) ste.getCause();
        assertThat(sfe).hasMessageThat().isEqualTo("Error dereferencing future");
        assertThat(sfe).hasCauseThat().isEqualTo(futureFailureCause);
        assertThat(ste).hasMessageThat().isEqualTo("When evaluating \"$content\": When evaluating \"$foo\": Error dereferencing future");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.transclusionCaller(no-path:17)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.transclusionCallee(no-path:23)");
        assertThat(ste.getStackTrace()[2].toString()).isEqualTo("ns.transclusionCaller(no-path:16)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) SoyFutureException(com.google.template.soy.data.SoyFutureException) SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyFutureException(com.google.template.soy.data.SoyFutureException) Test(org.junit.Test)

Example 3 with SoyTofuException

use of com.google.template.soy.tofu.SoyTofuException in project closure-templates by google.

the class TofuExceptionsTest method testExceptions_undefined.

@Test
public void testExceptions_undefined() throws Exception {
    SoyDict data = SoyValueConverterUtility.newDict("foo.boo", 42);
    // This is an exception that occurs during expression evaluation
    try {
        tofu.newRenderer("ns.callerTemplate").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasCauseThat().isNull();
        assertThat(ste).hasMessageThat().isEqualTo("In 'print' tag, expression \"$foo.bad\" evaluates to undefined.");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.calleeTemplate(no-path:11)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.callerTemplate(no-path:5)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 4 with SoyTofuException

use of com.google.template.soy.tofu.SoyTofuException in project closure-templates by google.

the class TofuExceptionsTest method testExceptions_transclusion_wrongTypeFuture.

@Test
public void testExceptions_transclusion_wrongTypeFuture() {
    SoyDict data = SoyValueConverterUtility.newDict("foo", Futures.immediateFuture("not an int"));
    try {
        tofu.newRenderer("ns.transclusionCaller").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasCauseThat().isNull();
        assertThat(ste).hasMessageThat().isEqualTo("When evaluating \"$content\": When evaluating \"$foo\": Parameter type mismatch: " + "attempt to bind value 'not an int' (a StringData) to parameter 'foo' which " + "has a declared type of 'int'.");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.transclusionCaller(no-path:14)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.transclusionCaller(no-path:17)");
        assertThat(ste.getStackTrace()[2].toString()).isEqualTo("ns.transclusionCallee(no-path:23)");
        assertThat(ste.getStackTrace()[3].toString()).isEqualTo("ns.transclusionCaller(no-path:16)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 5 with SoyTofuException

use of com.google.template.soy.tofu.SoyTofuException in project closure-templates by google.

the class TofuExceptionsTest method testExceptions_badType.

@Test
public void testExceptions_badType() throws Exception {
    SoyDict data = SoyValueConverterUtility.newDict("foo", "not a record");
    // This is an exception that occurs during template calling due to a type checkin
    try {
        tofu.newRenderer("ns.callerTemplate").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasCauseThat().isNull();
        assertThat(ste).hasMessageThat().isEqualTo("Parameter type mismatch: attempt to bind value 'not a record' (a StringData) to " + "parameter 'foo' which has a declared type of '[bad: string, boo: int]'.");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.calleeTemplate(no-path:8)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.callerTemplate(no-path:5)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Aggregations

SoyTofuException (com.google.template.soy.tofu.SoyTofuException)7 SoyDict (com.google.template.soy.data.SoyDict)6 Test (org.junit.Test)6 SoyFutureException (com.google.template.soy.data.SoyFutureException)2 EvalVisitorFactoryImpl (com.google.template.soy.sharedpasses.render.EvalVisitorFactoryImpl)1 RenderException (com.google.template.soy.sharedpasses.render.RenderException)1 RenderVisitor (com.google.template.soy.sharedpasses.render.RenderVisitor)1 TemplateNode (com.google.template.soy.soytree.TemplateNode)1