Bug fixed (part of FREEMARKER-48): When an arithmetic exception has occurred in an expression (typically division by zero), the template processing has thrown the ArithmeticException as is, without packaging it into a TemplateException. Thus, the error location in the template wasn't visible in the exception.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/b246de24 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/b246de24 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/b246de24 Branch: refs/heads/2.3 Commit: b246de2403bac5b82c44a83cc3b7ab5b6617d0e8 Parents: ee1e922 Author: ddekany <[email protected]> Authored: Mon Sep 18 22:49:56 2017 +0200 Committer: ddekany <[email protected]> Committed: Mon Sep 18 22:49:56 2017 +0200 ---------------------------------------------------------------------- .../freemarker/core/ArithmeticExpression.java | 38 +++++++++++--------- src/manual/en_US/book.xml | 11 ++++++ .../freemarker/core/MiscErrorMessagesTest.java | 11 ++++++ .../core/StringLiteralInterpolationTest.java | 2 +- 4 files changed, 45 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b246de24/src/main/java/freemarker/core/ArithmeticExpression.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/ArithmeticExpression.java b/src/main/java/freemarker/core/ArithmeticExpression.java index 4fbe778..2ed7890 100644 --- a/src/main/java/freemarker/core/ArithmeticExpression.java +++ b/src/main/java/freemarker/core/ArithmeticExpression.java @@ -54,22 +54,28 @@ final class ArithmeticExpression extends Expression { static TemplateModel _eval(Environment env, TemplateObject parent, Number lhoNumber, int operator, Number rhoNumber) throws TemplateException, _MiscTemplateException { ArithmeticEngine ae = EvalUtil.getArithmeticEngine(env, parent); - switch (operator) { - case TYPE_SUBSTRACTION : - return new SimpleNumber(ae.subtract(lhoNumber, rhoNumber)); - case TYPE_MULTIPLICATION : - return new SimpleNumber(ae.multiply(lhoNumber, rhoNumber)); - case TYPE_DIVISION : - return new SimpleNumber(ae.divide(lhoNumber, rhoNumber)); - case TYPE_MODULO : - return new SimpleNumber(ae.modulus(lhoNumber, rhoNumber)); - default: - if (parent instanceof Expression) { - throw new _MiscTemplateException((Expression) parent, - "Unknown operation: ", Integer.valueOf(operator)); - } else { - throw new _MiscTemplateException("Unknown operation: ", Integer.valueOf(operator)); - } + try { + switch (operator) { + case TYPE_SUBSTRACTION : + return new SimpleNumber(ae.subtract(lhoNumber, rhoNumber)); + case TYPE_MULTIPLICATION : + return new SimpleNumber(ae.multiply(lhoNumber, rhoNumber)); + case TYPE_DIVISION : + return new SimpleNumber(ae.divide(lhoNumber, rhoNumber)); + case TYPE_MODULO : + return new SimpleNumber(ae.modulus(lhoNumber, rhoNumber)); + default: + if (parent instanceof Expression) { + throw new _MiscTemplateException((Expression) parent, + "Unknown operation: ", Integer.valueOf(operator)); + } else { + throw new _MiscTemplateException("Unknown operation: ", Integer.valueOf(operator)); + } + } + } catch (ArithmeticException e) { + throw new _MiscTemplateException(e, env, + "Arithmetic operation failed", + (e.getMessage() != null ? new String[] { ": ", e.getMessage() } : " (see cause exception)")); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b246de24/src/manual/en_US/book.xml ---------------------------------------------------------------------- diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml index 210f674..edb158b 100644 --- a/src/manual/en_US/book.xml +++ b/src/manual/en_US/book.xml @@ -27179,6 +27179,17 @@ TemplateModel x = env.getVariable("x"); // get variable x</programlisting> </listitem> <listitem> + <para>Bug fixed (part of <link + xlink:href="https://issues.apache.org/jira/browse/FREEMARKER-48">FREEMARKER-48</link>): + When an arithmetic exception has occurred in an expression + (typically division by zero), the template processing has thrown + the <literal>ArithmeticException</literal> as is, without + packaging it into a <literal>TemplateException</literal>. Thus, + the error location in the template wasn't visible in the + exception.</para> + </listitem> + + <listitem> <para>When logging error due to an error in an <link linkend="ref.directive.attempt"><literal>attempt</literal> directive</link> block, the log message now indicates that the http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b246de24/src/test/java/freemarker/core/MiscErrorMessagesTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/core/MiscErrorMessagesTest.java b/src/test/java/freemarker/core/MiscErrorMessagesTest.java index 79ccb2c..5799c12 100644 --- a/src/test/java/freemarker/core/MiscErrorMessagesTest.java +++ b/src/test/java/freemarker/core/MiscErrorMessagesTest.java @@ -19,9 +19,13 @@ package freemarker.core; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + import org.junit.Test; import freemarker.cache.TemplateNameFormat; +import freemarker.template.TemplateException; import freemarker.test.TemplateTest; public class MiscErrorMessagesTest extends TemplateTest { @@ -45,4 +49,11 @@ public class MiscErrorMessagesTest extends TemplateTest { assertErrorContains("${{}[10]}", "[]", "?api"); } + @Test + public void aritheticException() { + Throwable e = assertErrorContains("<#assign x = 0>\n${1 / x}", "Arithmetic"); + assertThat(e, instanceOf(TemplateException.class)); + assertEquals((Integer) 2, ((TemplateException) e).getLineNumber()); + } + } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b246de24/src/test/java/freemarker/core/StringLiteralInterpolationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/core/StringLiteralInterpolationTest.java b/src/test/java/freemarker/core/StringLiteralInterpolationTest.java index b0d0581..f655865 100644 --- a/src/test/java/freemarker/core/StringLiteralInterpolationTest.java +++ b/src/test/java/freemarker/core/StringLiteralInterpolationTest.java @@ -80,7 +80,7 @@ public class StringLiteralInterpolationTest extends TemplateTest { public void testErrors() { addToDataModel("x", 1); assertErrorContains("${'${noSuchVar}'}", InvalidReferenceException.class, "missing", "noSuchVar"); - assertErrorContains("${'${x/0}'}", ArithmeticException.class, "zero"); + assertErrorContains("${'${x/0}'}", "zero"); } @Test
