You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2018/02/04 12:10:05 UTC

[1/4] incubator-freemarker git commit: Improved exceptions when calling JSP tags: Wrapping of non-TemplateModelException TemplateException-s (typically InvalidReferenceException-s) into TemplateModelException-s is now avoided when the TemplateException o

Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3 4ffe4b354 -> bc1455f52


Improved exceptions when calling JSP tags: Wrapping of non-TemplateModelException TemplateException-s (typically InvalidReferenceException-s) into TemplateModelException-s is now avoided when the TemplateException occurs in the body of a JSP tag.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/28561441
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/28561441
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/28561441

Branch: refs/heads/2.3
Commit: 285614418263bbf92504692aad6a754ced2c312b
Parents: 5f4b7f3
Author: ddekany <dd...@apache.org>
Authored: Sun Feb 4 12:11:45 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Sun Feb 4 12:14:58 2018 +0100

----------------------------------------------------------------------
 src/main/java/freemarker/core/Environment.java  |  6 +++
 .../freemarker/ext/jsp/JspTagModelBase.java     |  6 ++-
 .../template/TemplateModelException.java        | 37 ++++++++++++++++--
 src/manual/en_US/book.xml                       | 41 +++++++++++++-------
 .../basic/WEB-INF/expected/customTags1.txt      |  2 +-
 5 files changed, 72 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/28561441/src/main/java/freemarker/core/Environment.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java
index 762f646..310ae9d 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -875,6 +875,12 @@ public final class Environment extends Configurable {
 
     private void handleTemplateException(TemplateException templateException)
             throws TemplateException {
+        if (templateException instanceof TemplateModelException
+                && ((TemplateModelException) templateException).getReplaceWithCause()
+                && templateException.getCause() instanceof TemplateException) {
+            templateException = (TemplateException) templateException.getCause();
+        }
+        
         // Logic to prevent double-handling of the exception in
         // nested visit() calls.
         if (lastThrowable == templateException) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/28561441/src/main/java/freemarker/ext/jsp/JspTagModelBase.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/jsp/JspTagModelBase.java b/src/main/java/freemarker/ext/jsp/JspTagModelBase.java
index 83c0104..032cead 100644
--- a/src/main/java/freemarker/ext/jsp/JspTagModelBase.java
+++ b/src/main/java/freemarker/ext/jsp/JspTagModelBase.java
@@ -39,6 +39,7 @@ import freemarker.ext.jsp.SimpleTagDirectiveModel.TemplateExceptionWrapperJspExc
 import freemarker.template.ObjectWrapper;
 import freemarker.template.ObjectWrapperAndUnwrapper;
 import freemarker.template.Template;
+import freemarker.template.TemplateException;
 import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 import freemarker.template.utility.StringUtil;
@@ -145,8 +146,9 @@ class JspTagModelBase {
         if (e instanceof TemplateExceptionWrapperJspException) {
             return toTemplateModelExceptionOrRethrow(((TemplateExceptionWrapperJspException) e).getCause());
         }
-        return new _TemplateModelException(e,
-                "Error while invoking the ", new _DelayedJQuote(tagName), " JSP custom tag; see cause exception");
+        return new TemplateModelException(
+                "Error while invoking the " + StringUtil.jQuote(tagName) + " JSP custom tag; see cause exception",
+                e instanceof TemplateException, e);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/28561441/src/main/java/freemarker/template/TemplateModelException.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/TemplateModelException.java b/src/main/java/freemarker/template/TemplateModelException.java
index 1ab47b4..2926ba8 100644
--- a/src/main/java/freemarker/template/TemplateModelException.java
+++ b/src/main/java/freemarker/template/TemplateModelException.java
@@ -27,6 +27,8 @@ import freemarker.core._ErrorDescriptionBuilder;
  * if the requested data can't be retrieved.
  */
 public class TemplateModelException extends TemplateException {
+    
+    private final boolean replaceWithCause;
 
     /**
      * Constructs a <tt>TemplateModelException</tt> with no
@@ -64,14 +66,21 @@ public class TemplateModelException extends TemplateException {
     public TemplateModelException(Throwable cause) {
         this((String) null, cause);
     }
-
     
     /**
      * The same as {@link #TemplateModelException(String, Throwable)}; it's exists only for binary
      * backward-compatibility.
      */
     public TemplateModelException(String description, Exception cause) {
-        super(description, cause, null);
+        this(description, (Throwable) cause);
+    }
+
+    /**
+     * Same as {@link #TemplateModelException(String, boolean, Throwable)} with {@code false} {@code replaceWithCause}
+     * argument.
+     */
+    public TemplateModelException(String description, Throwable cause) {
+        this(description, false, cause);
     }
 
     /**
@@ -80,13 +89,18 @@ public class TemplateModelException extends TemplateException {
      * to be raised.
      *
      * @param description the description of the error that occurred
+     * @param replaceWithCause See {@link #getReplaceWithCause()}; usually {@code false}, unless you are forced to wrap
+     *     {@link TemplateException} into a {@link TemplateModelException} merely due to API constraints.
      * @param cause the underlying {@link Exception} that caused this
      * exception to be raised
+     * 
+     * @since 2.3.28
      */
-    public TemplateModelException(String description, Throwable cause) {
+    public TemplateModelException(String description, boolean replaceWithCause, Throwable cause) {
         super(description, cause, null);
+        this.replaceWithCause = replaceWithCause;
     }
-
+    
     /**
      * Don't use this; this is to be used internally by FreeMarker.
      * @param preventAmbiguity its value is ignored; it's only to prevent constructor selection ambiguities for
@@ -95,6 +109,7 @@ public class TemplateModelException extends TemplateException {
     protected TemplateModelException(Throwable cause, Environment env, String description,
             boolean preventAmbiguity) {
         super(description, cause, env);
+        this.replaceWithCause = false;
     }
     
     /**
@@ -106,6 +121,20 @@ public class TemplateModelException extends TemplateException {
             Throwable cause, Environment env, _ErrorDescriptionBuilder descriptionBuilder,
             boolean preventAmbiguity) {
         super(cause, env, null, descriptionBuilder);
+        this.replaceWithCause = false;
+    }
+    
+    /**
+     * Indicates that the cause exception should be thrown instead of this exception; it was only wrapped into this
+     * exception due to API constraints. Such unwanted wrapping typically occurs when you are only allowed to throw
+     * {@link TemplateModelException}, but the exception to propagate is a more generic {@link TemplateException}.
+     * The error handler mechanism of FreeMarker will replace the exception with its {@link #getCause()} when it has
+     * bubbled up to a place where that constraint doesn't apply anymore. 
+     * 
+     * @since 2.3.28
+     */
+    public boolean getReplaceWithCause() {
+        return replaceWithCause;
     }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/28561441/src/manual/en_US/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml
index 7325bd0..91c5893 100644
--- a/src/manual/en_US/book.xml
+++ b/src/manual/en_US/book.xml
@@ -27110,19 +27110,6 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
 
           <itemizedlist>
             <listitem>
-              <para>Bug fixed (<link
-              xlink:href="https://issues.apache.org/jira/browse/FREEMARKER-88">FREEMARKER-88</link>):
-              This is related to calling JSP tags from templates. If a
-              <literal>TemplateException</literal> that's not a
-              <literal>TemplateModelExceptoin</literal> has occurred in the
-              body of a JSP <literal>SimpleTag</literal> (typically, an
-              <literal>InvalidReferenceException</literal>), that has caused a
-              <literal>ClassCastException</literal> in the exception handling
-              code, thus the template processing has thrown that instead of
-              the original exception.</para>
-            </listitem>
-
-            <listitem>
               <para>Added new property to
               <literal>BeansWrapper.MethodAppearanceDecision</literal>:
               <literal>replaceExistingProperty</literal>. This is useful when
@@ -27134,6 +27121,34 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
               the real property isn't replaced, but now with
               <literal>replaceExistingProperty</literal> it can be.</para>
             </listitem>
+
+            <listitem>
+              <para>Improved exception handling when calling JSP tags:</para>
+
+              <itemizedlist>
+                <listitem>
+                  <para>Bug fixed (<link
+                  xlink:href="https://issues.apache.org/jira/browse/FREEMARKER-88">FREEMARKER-88</link>):
+                  If a <literal>TemplateException</literal> that's not a
+                  <literal>TemplateModelExceptoin</literal> has occurred in
+                  the body of a JSP <literal>SimpleTag</literal> (typically,
+                  an <literal>InvalidReferenceException</literal>), that has
+                  caused a <literal>ClassCastException</literal> in the
+                  exception handling code, thus the template processing has
+                  thrown that instead of the original exception.</para>
+                </listitem>
+
+                <listitem>
+                  <para>Wrapping of
+                  non-<literal>TemplateModelException</literal>
+                  <literal>TemplateException</literal>-s (typically
+                  <literal>InvalidReferenceException</literal>-s) into
+                  <literal>TemplateModelException</literal>-s is now avoided
+                  when the <literal>TemplateException</literal> occurs in the
+                  body of a JSP tag.</para>
+                </listitem>
+              </itemizedlist>
+            </listitem>
           </itemizedlist>
         </section>
       </section>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/28561441/src/test/resources/freemarker/ext/jsp/webapps/basic/WEB-INF/expected/customTags1.txt
----------------------------------------------------------------------
diff --git a/src/test/resources/freemarker/ext/jsp/webapps/basic/WEB-INF/expected/customTags1.txt b/src/test/resources/freemarker/ext/jsp/webapps/basic/WEB-INF/expected/customTags1.txt
index 54e6650..6a25e32 100644
--- a/src/test/resources/freemarker/ext/jsp/webapps/basic/WEB-INF/expected/customTags1.txt
+++ b/src/test/resources/freemarker/ext/jsp/webapps/basic/WEB-INF/expected/customTags1.txt
@@ -35,7 +35,7 @@ doFinally() called here
 
 <!-- Test abrupt execution -->
 doStartTag() called here
-doCatch() called here with class freemarker.core._TemplateModelException: Error while invoking the "testtag" JSP custom tag; see cause exception
+doCatch() called here with class freemarker.template.TemplateModelException: Error while invoking the "testtag" JSP custom tag; see cause exception
 doFinally() called here
 
 <!-- Test nested execution -->


[2/4] incubator-freemarker git commit: Bug fixed: For JSP Tag based custom tags, if an exception has occurred in the body (nested content), an IndexOutOfBoundsException might have occurred, replacing the original exception.

Posted by dd...@apache.org.
Bug fixed: For JSP Tag based custom tags, if an exception has occurred in the body (nested content), an IndexOutOfBoundsException might have occurred, replacing the original 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/db7c6d54
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/db7c6d54
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/db7c6d54

Branch: refs/heads/2.3
Commit: db7c6d5470b66af743ef1600aca4168852540e2e
Parents: 2856144
Author: ddekany <dd...@apache.org>
Authored: Sun Feb 4 12:53:27 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Sun Feb 4 12:53:27 2018 +0100

----------------------------------------------------------------------
 .../freemarker/ext/jsp/TagTransformModel.java   | 28 +++++++++++++-------
 src/manual/en_US/book.xml                       | 14 +++++++---
 2 files changed, 29 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/db7c6d54/src/main/java/freemarker/ext/jsp/TagTransformModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/jsp/TagTransformModel.java b/src/main/java/freemarker/ext/jsp/TagTransformModel.java
index bc0fda3..0190948 100644
--- a/src/main/java/freemarker/ext/jsp/TagTransformModel.java
+++ b/src/main/java/freemarker/ext/jsp/TagTransformModel.java
@@ -292,6 +292,7 @@ class TagTransformModel extends JspTagModelBase implements TemplateTransformMode
         private final FreeMarkerPageContext pageContext;
         private boolean needPop = true;
         private final boolean needDoublePop;
+        private boolean colosed = false;
         
         TagWriter(Writer out, Tag tag, FreeMarkerPageContext pageContext, boolean needDoublePop) {
             super((JspWriter) out, false);
@@ -393,20 +394,27 @@ class TagTransformModel extends JspTagModelBase implements TemplateTransformMode
         
         @Override
         public void close() {
-            if (needPop) {
-                pageContext.popWriter();
+            if (colosed) {
+                return;
             }
-            pageContext.popTopTag();
             try {
-                if (isTryCatchFinally) {
-                    ((TryCatchFinally) tag).doFinally();
-                }
-                // No pooling yet
-                tag.release();
-            } finally {
-                if (needDoublePop) {
+                if (needPop) {
                     pageContext.popWriter();
                 }
+                pageContext.popTopTag();
+                try {
+                    if (isTryCatchFinally) {
+                        ((TryCatchFinally) tag).doFinally();
+                    }
+                    // No pooling yet
+                    tag.release();
+                } finally {
+                    if (needDoublePop) {
+                        pageContext.popWriter();
+                    }
+                }
+            } finally {
+                colosed = true;
             }
         }
         

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/db7c6d54/src/manual/en_US/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml
index 91c5893..b208ecd 100644
--- a/src/manual/en_US/book.xml
+++ b/src/manual/en_US/book.xml
@@ -27123,7 +27123,7 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
             </listitem>
 
             <listitem>
-              <para>Improved exception handling when calling JSP tags:</para>
+              <para>Fixes in exception handling when calling JSP tags:</para>
 
               <itemizedlist>
                 <listitem>
@@ -27131,14 +27131,22 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
                   xlink:href="https://issues.apache.org/jira/browse/FREEMARKER-88">FREEMARKER-88</link>):
                   If a <literal>TemplateException</literal> that's not a
                   <literal>TemplateModelExceptoin</literal> has occurred in
-                  the body of a JSP <literal>SimpleTag</literal> (typically,
-                  an <literal>InvalidReferenceException</literal>), that has
+                  the body (nested content) of a JSP
+                  <literal>SimpleTag</literal> (typically, an
+                  <literal>InvalidReferenceException</literal>), that has
                   caused a <literal>ClassCastException</literal> in the
                   exception handling code, thus the template processing has
                   thrown that instead of the original exception.</para>
                 </listitem>
 
                 <listitem>
+                  <para>Bug fixed: For JSP Tag based custom tags, if an
+                  exception has occurred in the body (nested content), an
+                  <literal>IndexOutOfBoundsException</literal> might have
+                  occurred, replacing the original exception.</para>
+                </listitem>
+
+                <listitem>
                   <para>Wrapping of
                   non-<literal>TemplateModelException</literal>
                   <literal>TemplateException</literal>-s (typically


[3/4] incubator-freemarker git commit: Code cleanup for last commit...

Posted by dd...@apache.org.
Code cleanup for last commit...


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/4ba637f1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/4ba637f1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/4ba637f1

Branch: refs/heads/2.3
Commit: 4ba637f15948ce5d4bec7e8a1ecaacd4d4502e3c
Parents: db7c6d5
Author: ddekany <dd...@apache.org>
Authored: Sun Feb 4 13:06:43 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Sun Feb 4 13:06:43 2018 +0100

----------------------------------------------------------------------
 .../freemarker/ext/jsp/TagTransformModel.java   | 32 +++++++++-----------
 1 file changed, 15 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4ba637f1/src/main/java/freemarker/ext/jsp/TagTransformModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/jsp/TagTransformModel.java b/src/main/java/freemarker/ext/jsp/TagTransformModel.java
index 0190948..55b0272 100644
--- a/src/main/java/freemarker/ext/jsp/TagTransformModel.java
+++ b/src/main/java/freemarker/ext/jsp/TagTransformModel.java
@@ -292,7 +292,7 @@ class TagTransformModel extends JspTagModelBase implements TemplateTransformMode
         private final FreeMarkerPageContext pageContext;
         private boolean needPop = true;
         private final boolean needDoublePop;
-        private boolean colosed = false;
+        private boolean closed = false;
         
         TagWriter(Writer out, Tag tag, FreeMarkerPageContext pageContext, boolean needDoublePop) {
             super((JspWriter) out, false);
@@ -394,27 +394,25 @@ class TagTransformModel extends JspTagModelBase implements TemplateTransformMode
         
         @Override
         public void close() {
-            if (colosed) {
+            if (closed) {
                 return;
             }
+            closed = true;
+            
+            if (needPop) {
+                pageContext.popWriter();
+            }
+            pageContext.popTopTag();
             try {
-                if (needPop) {
-                    pageContext.popWriter();
-                }
-                pageContext.popTopTag();
-                try {
-                    if (isTryCatchFinally) {
-                        ((TryCatchFinally) tag).doFinally();
-                    }
-                    // No pooling yet
-                    tag.release();
-                } finally {
-                    if (needDoublePop) {
-                        pageContext.popWriter();
-                    }
+                if (isTryCatchFinally) {
+                    ((TryCatchFinally) tag).doFinally();
                 }
+                // No pooling yet
+                tag.release();
             } finally {
-                colosed = true;
+                if (needDoublePop) {
+                    pageContext.popWriter();
+                }
             }
         }
         


[4/4] incubator-freemarker git commit: Merge remote-tracking branch 'origin/2.3-gae' into 2.3

Posted by dd...@apache.org.
Merge remote-tracking branch 'origin/2.3-gae' into 2.3


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/bc1455f5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/bc1455f5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/bc1455f5

Branch: refs/heads/2.3
Commit: bc1455f5281b1315239fad0a683e425b8efbc062
Parents: 4ffe4b3 4ba637f
Author: ddekany <dd...@apache.org>
Authored: Sun Feb 4 13:09:45 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Sun Feb 4 13:09:45 2018 +0100

----------------------------------------------------------------------
 src/main/java/freemarker/core/Environment.java  |  6 +++
 .../freemarker/ext/jsp/JspTagModelBase.java     |  6 ++-
 .../freemarker/ext/jsp/TagTransformModel.java   |  6 +++
 .../template/TemplateModelException.java        | 37 +++++++++++++--
 src/manual/en_US/book.xml                       | 49 ++++++++++++++------
 .../basic/WEB-INF/expected/customTags1.txt      |  2 +-
 6 files changed, 86 insertions(+), 20 deletions(-)
----------------------------------------------------------------------