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 2021/11/08 00:55:59 UTC
[freemarker] 01/03: Better error messages in case a custom
formatter forgets to wrap a runtime exception into
TemplateValueFormatException.
This is an automated email from the ASF dual-hosted git repository.
ddekany pushed a commit to branch FREEMARKER-35
in repository https://gitbox.apache.org/repos/asf/freemarker.git
commit 9cbf867df3f5d0d824dfa384312af6176f96cb1a
Author: ddekany <dd...@apache.org>
AuthorDate: Sun Nov 7 17:47:45 2021 +0100
Better error messages in case a custom formatter forgets to wrap a runtime exception into TemplateValueFormatException.
---
src/main/java/freemarker/core/Environment.java | 36 ++++++++++++++++---------
src/main/java/freemarker/core/_MessageUtil.java | 33 ++++++++++++++---------
2 files changed, 44 insertions(+), 25 deletions(-)
diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java
index ea90bb0..1208e79 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -1461,11 +1461,13 @@ public final class Environment extends Configurable {
*/
Object formatNumber(TemplateNumberModel number, Expression exp, boolean useTempModelExc) throws TemplateException {
TemplateNumberFormat format = getTemplateNumberFormat(exp, false);
+ Object result;
try {
- return EvalUtil.assertFormatResultNotNull(format.format(number));
- } catch (TemplateValueFormatException e) {
+ result = format.format(number);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
@@ -1489,11 +1491,13 @@ public final class Environment extends Configurable {
TemplateNumberModel number, TemplateNumberFormat format, Expression exp,
boolean useTempModelExc)
throws TemplateException {
+ String result;
try {
- return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number));
- } catch (TemplateValueFormatException e) {
+ result = format.formatToPlainText(number);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
@@ -1776,11 +1780,13 @@ public final class Environment extends Configurable {
Object formatDate(TemplateDateModel tdm, Expression blamedTdmSourceExpr, boolean useTempModelExc)
throws TemplateException {
TemplateDateFormat format = getTemplateDateFormat(tdm, blamedTdmSourceExpr, useTempModelExc);
+ Object result;
try {
- return EvalUtil.assertFormatResultNotNull(format.format(tdm));
- } catch (TemplateValueFormatException e) {
+ result = format.format(tdm);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatDateException(format, blamedTdmSourceExpr, e, useTempModelExc);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
@@ -1815,11 +1821,13 @@ public final class Environment extends Configurable {
static String formatDateToPlainText(
TemplateDateModel tdm, Expression blamedTdmSourceExp, TemplateDateFormat format,
boolean useTempModelExc) throws TemplateException {
+ String result;
try {
- return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(tdm));
- } catch (TemplateValueFormatException e) {
+ result = format.formatToPlainText(tdm);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatDateException(format, blamedTdmSourceExp, e, useTempModelExc);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
@@ -2257,11 +2265,13 @@ public final class Environment extends Configurable {
Object formatTemporal(TemplateTemporalModel ttm, Expression blamedTtmSourceExp, boolean useTempModelExc)
throws TemplateException {
TemplateTemporalFormat format = getTemplateTemporalFormat(ttm, blamedTtmSourceExp, useTempModelExc);
+ Object result;
try {
- return EvalUtil.assertFormatResultNotNull(format.format(ttm));
- } catch (TemplateValueFormatException e) {
+ result = format.format(ttm);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatTemporalException(format, ttm, blamedTtmSourceExp, e, useTempModelExc);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
@@ -2295,11 +2305,13 @@ public final class Environment extends Configurable {
TemplateTemporalModel ttm, Expression blamedTtmSourceExp, TemplateTemporalFormat ttf,
boolean useTempModelExc)
throws TemplateException {
+ String result;
try {
- return EvalUtil.assertFormatResultNotNull(ttf.formatToPlainText(ttm));
- } catch (TemplateValueFormatException e) {
+ result = ttf.formatToPlainText(ttm);
+ } catch (Exception e) {
throw _MessageUtil.newCantFormatTemporalException(ttf, ttm, blamedTtmSourceExp, e, useTempModelExc);
}
+ return EvalUtil.assertFormatResultNotNull(result);
}
/**
diff --git a/src/main/java/freemarker/core/_MessageUtil.java b/src/main/java/freemarker/core/_MessageUtil.java
index 74b4664..0d5093d 100644
--- a/src/main/java/freemarker/core/_MessageUtil.java
+++ b/src/main/java/freemarker/core/_MessageUtil.java
@@ -314,27 +314,32 @@ public class _MessageUtil {
}
public static TemplateException newCantFormatDateException(TemplateDateFormat format, Expression dataSrcExp,
- TemplateValueFormatException e, boolean useTempModelExc) {
+ Exception formattingException, boolean useTempModelExc) {
_ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
"Failed to format date/time/datetime with format ", new _DelayedJQuote(format.getDescription()), ": ",
- e.getMessage())
+ formattingException instanceof TemplateValueFormatException
+ ? formattingException.getMessage()
+ : String.valueOf(formattingException))
.blame(dataSrcExp);
return useTempModelExc
- ? new _TemplateModelException(e, null, desc)
- : new _MiscTemplateException(e, null, desc);
+ ? new _TemplateModelException(formattingException, null, desc)
+ : new _MiscTemplateException(formattingException, null, desc);
}
- public static TemplateException newCantFormatTemporalException(TemplateTemporalFormat format, TemplateTemporalModel ttm, Expression dataSrcExp,
- TemplateValueFormatException e, boolean useTempModelExc) {
+ public static TemplateException newCantFormatTemporalException(
+ TemplateTemporalFormat format, TemplateTemporalModel ttm, Expression dataSrcExp,
+ Exception formattingException, boolean useTempModelExc) {
_ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
"Failed to format temporal value of class ", safeGetTemporalClass(ttm),
", value ", new _DelayedJQuote(new _DelayedToString(safeGetTemporalValue(ttm))),
", with format ", new _DelayedJQuote(format.getDescription()), ": ",
- e.getMessage())
+ formattingException instanceof TemplateValueFormatException
+ ? formattingException.getMessage()
+ : String.valueOf(formattingException))
.blame(dataSrcExp);
return useTempModelExc
- ? new _TemplateModelException(e, null, desc)
- : new _MiscTemplateException(e, null, desc);
+ ? new _TemplateModelException(formattingException, null, desc)
+ : new _MiscTemplateException(formattingException, null, desc);
}
private static String safeGetTemporalClass(TemplateTemporalModel ttm) {
@@ -355,14 +360,16 @@ public class _MessageUtil {
}
public static TemplateException newCantFormatNumberException(TemplateNumberFormat format, Expression dataSrcExp,
- TemplateValueFormatException e, boolean useTempModelExc) {
+ Exception formattingException, boolean useTempModelExc) {
_ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
"Failed to format number with format ", new _DelayedJQuote(format.getDescription()), ": ",
- e.getMessage())
+ formattingException instanceof TemplateValueFormatException
+ ? formattingException.getMessage()
+ : String.valueOf(formattingException))
.blame(dataSrcExp);
return useTempModelExc
- ? new _TemplateModelException(e, null, desc)
- : new _MiscTemplateException(e, null, desc);
+ ? new _TemplateModelException(formattingException, null, desc)
+ : new _MiscTemplateException(formattingException, null, desc);
}
public static TemplateModelException newKeyValuePairListingNonStringKeyExceptionMessage(