From notifications-return-4999-archive-asf-public=cust-asf.ponee.io@freemarker.incubator.apache.org Sun Feb 4 13:10:14 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 2C62918064A for ; Sun, 4 Feb 2018 13:10:14 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 1BC16160C40; Sun, 4 Feb 2018 12:10:14 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1A53B160C37 for ; Sun, 4 Feb 2018 13:10:12 +0100 (CET) Received: (qmail 64448 invoked by uid 500); 4 Feb 2018 12:10:12 -0000 Mailing-List: contact notifications-help@freemarker.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.incubator.apache.org Delivered-To: mailing list notifications@freemarker.incubator.apache.org Received: (qmail 64439 invoked by uid 99); 4 Feb 2018 12:10:12 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Feb 2018 12:10:12 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id E0F0B1807AA for ; Sun, 4 Feb 2018 12:10:11 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -11.731 X-Spam-Level: X-Spam-Status: No, score=-11.731 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01, USER_IN_DEF_SPF_WL=-7.5] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id YbQiDARKsrmk for ; Sun, 4 Feb 2018 12:10:09 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id F20405FB9B for ; Sun, 4 Feb 2018 12:10:07 +0000 (UTC) Received: (qmail 64284 invoked by uid 99); 4 Feb 2018 12:10:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Feb 2018 12:10:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 050DDE9654; Sun, 4 Feb 2018 12:10:05 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ddekany@apache.org To: notifications@freemarker.incubator.apache.org Date: Sun, 04 Feb 2018 12:10:05 -0000 Message-Id: <8d7efbc6befd4def91b6c0d7d39c3b13@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [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 Authored: Sun Feb 4 12:11:45 2018 +0100 Committer: ddekany 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 TemplateModelException 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 - Bug fixed (FREEMARKER-88): - This is related to calling JSP tags from templates. If a - TemplateException that's not a - TemplateModelExceptoin has occurred in the - body of a JSP SimpleTag (typically, an - InvalidReferenceException), that has caused a - ClassCastException in the exception handling - code, thus the template processing has thrown that instead of - the original exception. - - - Added new property to BeansWrapper.MethodAppearanceDecision: replaceExistingProperty. This is useful when @@ -27134,6 +27121,34 @@ TemplateModel x = env.getVariable("x"); // get variable x the real property isn't replaced, but now with replaceExistingProperty it can be. + + + Improved exception handling when calling JSP tags: + + + + Bug fixed (FREEMARKER-88): + If a TemplateException that's not a + TemplateModelExceptoin has occurred in + the body of a JSP SimpleTag (typically, + an InvalidReferenceException), that has + caused a ClassCastException in the + exception handling code, thus the template processing has + thrown that instead of the original exception. + + + + 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. + + + 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 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