freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [5/6] incubator-freemarker git commit: Fixed doubled FTL stack trace in `?string` formatting errors
Date Tue, 08 Sep 2015 22:14:43 GMT
Fixed doubled FTL stack trace in `?string` formatting errors


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

Branch: refs/heads/2.3
Commit: 8983313236a7dc72fc76be26a8e53d4512a94324
Parents: 52dcb97
Author: ddekany <ddekany@apache.org>
Authored: Tue Sep 8 00:17:29 2015 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Tue Sep 8 00:17:29 2015 +0200

----------------------------------------------------------------------
 .../core/BuiltInsForMultipleTypes.java          | 67 ++++++++---------
 src/main/java/freemarker/core/Environment.java  | 79 +++++++++++++-------
 src/main/java/freemarker/core/EvalUtil.java     |  4 +-
 .../java/freemarker/core/NumberLiteral.java     |  2 +-
 src/main/java/freemarker/core/_CoreAPI.java     |  4 +-
 5 files changed, 86 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/89833132/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java b/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
index fe22206..55fa6b3 100644
--- a/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
+++ b/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
@@ -145,15 +145,10 @@ class BuiltInsForMultipleTypes {
             private Date cachedValue;
             
             DateParser(String text, Environment env)
-            throws TemplateModelException {
+            throws TemplateException {
                 this.text = text;
                 this.env = env;
-                try {
-                    this.defaultFormat = env.getTemplateDateFormat(dateType, Date.class,
target);
-                } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
-                }
+                this.defaultFormat = env.getTemplateDateFormat(dateType, Date.class, target,
false);
             }
             
             public Object exec(List args) throws TemplateModelException {
@@ -164,10 +159,10 @@ class BuiltInsForMultipleTypes {
             public TemplateModel get(String pattern) throws TemplateModelException {
                 TemplateDateFormat format;
                 try {
-                    format = env.getTemplateDateFormat(pattern, dateType, Date.class, target,
dateBI.this);
+                    format = env.getTemplateDateFormat(pattern, dateType, Date.class, target,
dateBI.this, true);
                 } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
+                    // `e` should always be a TemplateModelException here, but to be sure:

+                    throw _CoreAPI.ensureIsTemplateModelException("Failed to get format",
e); 
                 }
                 return new SimpleDate(parse(format), dateType);
             }
@@ -525,34 +520,34 @@ class BuiltInsForMultipleTypes {
             private String cachedValue;
     
             DateFormatter(TemplateDateModel dateModel, Environment env)
-            throws TemplateModelException {
+            throws TemplateException {
                 this.dateModel = dateModel;
                 this.env = env;
                 
                 final int dateType = dateModel.getDateType();
-                try {
-                    this.defaultFormat = dateType == TemplateDateModel.UNKNOWN
-                            ? null  // Lazy unknown type error in getAsString()
-                            : env.getTemplateDateFormat(
-                                    dateType, EvalUtil.modelToDate(dateModel, target).getClass(),
target);
-                } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
-                }
+                this.defaultFormat = dateType == TemplateDateModel.UNKNOWN
+                        ? null  // Lazy unknown type error in getAsString()
+                        : env.getTemplateDateFormat(
+                                dateType, EvalUtil.modelToDate(dateModel, target).getClass(),
target, true);
             }
     
             public Object exec(List args) throws TemplateModelException {
                 checkMethodArgCount(args, 1);
-                return get((String) args.get(0));
+                return formatWith((String) args.get(0));
             }
 
             public TemplateModel get(String key)
             throws TemplateModelException {
+                return formatWith(key);
+            }
+
+            private TemplateModel formatWith(String key)
+            throws TemplateModelException {
                 try {
-                    return new SimpleScalar(env.formatDate(dateModel, key, target, stringBI.this));
+                    return new SimpleScalar(env.formatDate(dateModel, key, target, stringBI.this,
true));
                 } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
+                    // `e` should always be a TemplateModelException here, but to be sure:

+                    throw _CoreAPI.ensureIsTemplateModelException("Failed to format value",
e); 
                 }
             }
             
@@ -598,10 +593,10 @@ class BuiltInsForMultipleTypes {
                 this.numberModel = numberModel;
                 number = EvalUtil.modelToNumber(numberModel, target);  // for BackwardCompatibleTemplateNumberFormat-s
                 try {
-                    defaultFormat = env.getTemplateNumberFormat(stringBI.this);
+                    defaultFormat = env.getTemplateNumberFormat(stringBI.this, true);
                 } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
+                    // `e` should always be a TemplateModelException here, but to be sure:

+                    throw _CoreAPI.ensureIsTemplateModelException("Failed to get default
number format", e); 
                 }
             }
     
@@ -613,10 +608,10 @@ class BuiltInsForMultipleTypes {
             public TemplateModel get(String key) throws TemplateModelException {
                 TemplateNumberFormat format;
                 try {
-                    format = env.getTemplateNumberFormat(key, stringBI.this);
+                    format = env.getTemplateNumberFormat(key, stringBI.this, true);
                 } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
+                    // `e` should always be a TemplateModelException here, but to be sure:

+                    throw _CoreAPI.ensureIsTemplateModelException("Failed to get number format",
e); 
                 }
                 
                 String result;
@@ -624,11 +619,11 @@ class BuiltInsForMultipleTypes {
                     if (format instanceof BackwardCompatibleTemplateNumberFormat) {
                         result = env.formatNumber(number, (BackwardCompatibleTemplateNumberFormat)
format, target);
                     } else {
-                        result = env.formatNumber(numberModel, format, target);
+                        result = env.formatNumber(numberModel, format, target, true);
                     }
                 } catch (TemplateException e) {
-                    // Must convert TemplateException-s to TemplateModelException-s due to
API restriction.
-                    throw _CoreAPI.convertToTemplateModelException(e); 
+                    // `e` should always be a TemplateModelException here, but to be sure:

+                    throw _CoreAPI.ensureIsTemplateModelException("Failed to format number",
e); 
                 }
                 
                 return new SimpleScalar(result);
@@ -641,11 +636,11 @@ class BuiltInsForMultipleTypes {
                             cachedValue = env.formatNumber(
                                     number, (BackwardCompatibleTemplateNumberFormat) defaultFormat,
target);
                         } else {
-                            cachedValue = env.formatNumber(numberModel, defaultFormat, target);
+                            cachedValue = env.formatNumber(numberModel, defaultFormat, target,
true);
                         }
                     } catch (TemplateException e) {
-                        // Must convert TemplateException-s to TemplateModelException-s due
to API restriction.
-                        throw _CoreAPI.convertToTemplateModelException(e); 
+                        // `e` should always be a TemplateModelException here, but to be
sure: 
+                        throw _CoreAPI.ensureIsTemplateModelException("Failed to format number",
e); 
                     }
                 }
                 return cachedValue;

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/89833132/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 b616b5e..1987353 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -1033,8 +1033,8 @@ public final class Environment extends Configurable {
      * @param exp
      *            The blamed expression if an error occurs; it's only needed for better error
messages
      */
-    String formatNumber(TemplateNumberModel number, Expression exp) throws TemplateException
{
-        return formatNumber(number, getTemplateNumberFormat(exp), exp);
+    String formatNumber(TemplateNumberModel number, Expression exp, boolean useTempModelExc)
throws TemplateException {
+        return formatNumber(number, getTemplateNumberFormat(exp, useTempModelExc), exp, useTempModelExc);
     }
 
     /**
@@ -1043,8 +1043,10 @@ public final class Environment extends Configurable {
      * @param exp
      *            The blamed expression if an error occurs; it's only needed for better error
messages
      */
-    String formatNumber(TemplateNumberModel number, String formatString, Expression exp)
throws TemplateException {
-        return formatNumber(number, getTemplateNumberFormat(formatString, exp), exp);
+    String formatNumber(
+            TemplateNumberModel number, String formatString, Expression exp,
+            boolean useTempModelExc) throws TemplateException {
+        return formatNumber(number, getTemplateNumberFormat(formatString, exp, useTempModelExc),
exp, useTempModelExc);
     }
 
     /**
@@ -1053,14 +1055,19 @@ public final class Environment extends Configurable {
      * @param exp
      *            The blamed expression if an error occurs; it's only needed for better error
messages
      */
-    String formatNumber(TemplateNumberModel number, TemplateNumberFormat format, Expression
exp)
-            throws TemplateModelException, _MiscTemplateException {
+    String formatNumber(
+            TemplateNumberModel number, TemplateNumberFormat format, Expression exp,
+            boolean useTempModelExc)
+            throws TemplateException {
         try {
             return format.format(number);
         } catch (UnformattableNumberException e) {
-            throw new _MiscTemplateException(exp, e, this,
+            _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
                     "Failed to format number with format ", new _DelayedJQuote(format.getDescription()),
": ",
-                    e.getMessage());
+                    e.getMessage())
+                    .blame(exp); 
+            throw useTempModelExc
+                    ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e,
this, desc);
         }
     }
 
@@ -1147,14 +1154,17 @@ public final class Environment extends Configurable {
     /**
      * Convenience wrapper around {@link #getTemplateNumberFormat()} to be called during
expression evaluation.
      */
-    TemplateNumberFormat getTemplateNumberFormat(Expression exp) throws _MiscTemplateException
{
+    TemplateNumberFormat getTemplateNumberFormat(Expression exp, boolean useTempModelExc)
throws TemplateException {
         TemplateNumberFormat format;
         try {
             format = getTemplateNumberFormat();
         } catch (InvalidFormatStringException e) {
-            throw new _MiscTemplateException(exp, e, this,
+            _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
                     "Failed to get number format object for the current number format string,
",
-                    new _DelayedJQuote(getNumberFormat()), ": " + e.getMessage());
+                    new _DelayedJQuote(getNumberFormat()), ": ", e.getMessage())
+                    .blame(exp); 
+            throw useTempModelExc
+                    ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e,
this, desc);
         }
         return format;
     }
@@ -1165,15 +1175,18 @@ public final class Environment extends Configurable {
      * @param exp
      *            The blamed expression if an error occurs; it's only needed for better error
messages
      */
-    TemplateNumberFormat getTemplateNumberFormat(String formatString, Expression exp)
-            throws _MiscTemplateException {
+    TemplateNumberFormat getTemplateNumberFormat(String formatString, Expression exp, boolean
useTempModelExc)
+            throws TemplateException {
         TemplateNumberFormat format;
         try {
             format = getTemplateNumberFormat(formatString);
         } catch (InvalidFormatStringException e) {
-            throw new _MiscTemplateException(exp, e, this,
+            _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
                     "Failed to get number format object for the ", new _DelayedJQuote(formatString),
-                    " number format string: " + e.getMessage());
+                    " number format string: ", e.getMessage())
+                    .blame(exp);
+            throw useTempModelExc
+                    ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e,
this, desc);
         }
         return format;
     }
@@ -1329,11 +1342,13 @@ public final class Environment extends Configurable {
      * @param tdmSourceExpr
      *            The blamed expression if an error occurs; only used for error messages.
      */
-    String formatDate(TemplateDateModel tdm, Expression tdmSourceExpr) throws TemplateException
{
+    String formatDate(TemplateDateModel tdm, Expression tdmSourceExpr,
+            boolean useTempModelExc) throws TemplateException {
         Date date = EvalUtil.modelToDate(tdm, tdmSourceExpr);
         
         TemplateDateFormat format = getTemplateDateFormat(
-                tdm.getDateType(), date.getClass(), tdmSourceExpr);
+                tdm.getDateType(), date.getClass(), tdmSourceExpr,
+                useTempModelExc);
         
         try {
             return format.format(tdm);
@@ -1349,12 +1364,14 @@ public final class Environment extends Configurable {
      *            The blamed expression if an error occurs; only used for error messages.
      */
     String formatDate(TemplateDateModel tdm, String formatString,
-            Expression blamedDateSourceExp, Expression blamedFormatterExp) throws TemplateException
{
+            Expression blamedDateSourceExp, Expression blamedFormatterExp,
+            boolean useTempModelExc) throws TemplateException {
         Date date = EvalUtil.modelToDate(tdm, blamedDateSourceExp);
         
         TemplateDateFormat format = getTemplateDateFormat(
                 formatString, tdm.getDateType(), date.getClass(),
-                blamedDateSourceExp, blamedFormatterExp);
+                blamedDateSourceExp, blamedFormatterExp,
+                useTempModelExc);
         
         try {
             return format.format(tdm);
@@ -1537,7 +1554,8 @@ public final class Environment extends Configurable {
      * Same as {@link #getTemplateDateFormat(int, Class)}, but translates the exceptions
to {@link TemplateException}-s.
      */
     TemplateDateFormat getTemplateDateFormat(
-            int dateType, Class<? extends Date> dateClass, Expression blamedDateSourceExp)
throws TemplateException {
+            int dateType, Class<? extends Date> dateClass, Expression blamedDateSourceExp,
boolean useTempModelExc)
+                    throws TemplateException {
         try {
             return getTemplateDateFormat(dateType, dateClass);
         } catch (UnknownDateTypeFormattingUnsupportedException e) {
@@ -1563,11 +1581,12 @@ public final class Environment extends Configurable {
                 settingValue = "???";
             }
             
-            throw new _MiscTemplateException(e,
+            _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
                     "The value of the \"", settingName,
                     "\" FreeMarker configuration setting is a malformed date/time/datetime
format string: ",
                     new _DelayedJQuote(settingValue), ". Reason given: ",
-                    e.getMessage());
+                    e.getMessage());                    
+            throw useTempModelExc ? new _TemplateModelException(e, desc) : new _MiscTemplateException(e,
desc);
         }
     }
 
@@ -1577,18 +1596,20 @@ public final class Environment extends Configurable {
      */
     TemplateDateFormat getTemplateDateFormat(
             String formatString, int dateType, Class<? extends Date> dateClass,
-            Expression blamedDateSourceExp, Expression blamedFormatterExp) throws TemplateException
{
+            Expression blamedDateSourceExp, Expression blamedFormatterExp,
+            boolean useTempModelExc)
+            throws TemplateException {
         try {
             return getTemplateDateFormat(formatString, dateType, dateClass);
         } catch (UnknownDateTypeFormattingUnsupportedException e) {
             throw MessageUtil.newCantFormatUnknownTypeDateException(blamedDateSourceExp,
e);
         } catch (InvalidFormatStringException e) {
-            throw new _MiscTemplateException(e,
-                    new _ErrorDescriptionBuilder(
-                            "Malformed date/time/datetime format string: ",
-                            new _DelayedJQuote(formatString), ". Reason given: ",
-                            e.getMessage())
-                            .blame(blamedFormatterExp));
+            _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
+                    "Malformed date/time/datetime format string: ",
+                    new _DelayedJQuote(formatString), ". Reason given: ",
+                    e.getMessage())
+                    .blame(blamedFormatterExp);
+            throw useTempModelExc ? new _TemplateModelException(e, desc) : new _MiscTemplateException(e,
desc);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/89833132/src/main/java/freemarker/core/EvalUtil.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/EvalUtil.java b/src/main/java/freemarker/core/EvalUtil.java
index bee90c9..ff41523 100644
--- a/src/main/java/freemarker/core/EvalUtil.java
+++ b/src/main/java/freemarker/core/EvalUtil.java
@@ -353,9 +353,9 @@ class EvalUtil {
             boolean allowTOM,
             Environment env) throws TemplateException {
         if (tm instanceof TemplateNumberModel) {
-            return env.formatNumber((TemplateNumberModel) tm, exp);
+            return env.formatNumber((TemplateNumberModel) tm, exp, false);
         } else if (tm instanceof TemplateDateModel) {
-            return env.formatDate((TemplateDateModel) tm, exp);
+            return env.formatDate((TemplateDateModel) tm, exp, false);
         } else if (allowTOM && tm instanceof TemplateMarkupOutputModel) {
             return null;
         } else if (tm instanceof TemplateScalarModel) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/89833132/src/main/java/freemarker/core/NumberLiteral.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/NumberLiteral.java b/src/main/java/freemarker/core/NumberLiteral.java
index b9b4171..f008aa9 100644
--- a/src/main/java/freemarker/core/NumberLiteral.java
+++ b/src/main/java/freemarker/core/NumberLiteral.java
@@ -43,7 +43,7 @@ final class NumberLiteral extends Expression implements TemplateNumberModel
{
 
     @Override
     public String evalAndCoerceToString(Environment env) throws TemplateException {
-        return env.formatNumber(this, this);
+        return env.formatNumber(this, this, false);
     }
 
     public Number getAsNumber() {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/89833132/src/main/java/freemarker/core/_CoreAPI.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/_CoreAPI.java b/src/main/java/freemarker/core/_CoreAPI.java
index 03429c8..620edd8 100644
--- a/src/main/java/freemarker/core/_CoreAPI.java
+++ b/src/main/java/freemarker/core/_CoreAPI.java
@@ -164,12 +164,12 @@ public class _CoreAPI {
      * The work around the problematic cases where we should throw a {@link TemplateException},
but we are inside
      * a {@link TemplateModel} method and so we can only throw {@link TemplateModelException}-s.
 
      */
-    public static TemplateModelException convertToTemplateModelException(TemplateException
e) {
+    public static TemplateModelException ensureIsTemplateModelException(String modelOpMsg,
TemplateException e) {
         if (e instanceof TemplateModelException) {
             return (TemplateModelException) e;
         } else {
             return new _TemplateModelException(
-                    _TemplateAPI.getBlamedExpression(e), e.getCause(), e.getEnvironment(),
e.getMessage());
+                    _TemplateAPI.getBlamedExpression(e), e.getCause(), e.getEnvironment(),
modelOpMsg);
         }
     }
     


Mime
View raw message